Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Update list based on presence of identifier in a second list
Post
Update list based on presence of identifier in a second list
In C#, I have two lists and need to mark records in the first based on the second. Here's a sample:
public class Vehicle
{
public string Make { get; set; }
public string VIN { get; set; }
public string Color { get; set; }
public bool HasRegistration { get; set; } = false;
}
The second list is simply a set of VINs (List<string>
) that have been registered. I need to mark all the vehicles in the List<Vehicle>
which have their VIN somewhere in the List<string>
. This is what I have, and it works, but I am looking for something more LINQ.
foreach(var v in vehicles)
{
if (vinList.Contains(v.VIN))
{
v.HasRegistration = true;
}
}
I could also do v.HasRegistration = (vinList.Contains(v.VIN));
Can this be done in a single, LINQ statement?
1 comment thread