Alex Rivera | Logout

How to determine if a string contains any matches of a list of strings

Asked 2011-03-23T22:23:46.970
17

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?

Edit
Report

2 Answers

1
var m = Vehicles.Where(v => listOfStrings.Contains(v.Name));
answered 2011-03-23T22:27:59.917
0

You can perform an Inner Join:

var matchingVehicles = from vehicle in vehicles
                       join item in listOfStrings on vehicle.Name equals item
                       select vehicle;
answered 2011-03-23T22:26:00.607

Your Answer