Hi say I have a list of strings:
var listOfStrings = new List<string>{"Cars", "Trucks", "Boats"};
and I have a vehicles options which has a Name field.
I want to find the vehicles where the name matches one of the items in the listOfStrings.
I'm trying to do this with linq but can't seem to finish it at the moment.
var matchingVehicles = Vehicles.Where(v => v.Name == one of the listOfStringItem)
Can anybody help me with this?
Vehicles.Where(v => listOfStrings.Contains(v.Name))
Use a HashSet
instead of a List
, that way you can look for a string without having to loop through the list.
var setOfStrings = new HashSet<string> {"Cars", "Trucks", "Boats"};
Now you can use the Contains
method to efficiently look for a match:
var matchingVehicles = Vehicles.Where(v => setOfStrings.Contains(v.Name));
would this work:
listOfStrings.Contains("Trucks");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With