[ACCEPTED]-How to determine if a string contains any matches of a list of strings-linq

Accepted answer
Score: 13
Vehicles.Where(v => listOfStrings.Contains(v.Name))

0

Score: 12

Use a HashSet instead of a List, that way you can look 3 for a string without having to loop through 2 the list.

var setOfStrings = new HashSet<string> {"Cars", "Trucks", "Boats"};

Now you can use the Contains method to 1 efficiently look for a match:

var matchingVehicles = Vehicles.Where(v => setOfStrings.Contains(v.Name));
Score: 6

would this work:

listOfStrings.Contains("Trucks");

0

Score: 1
var m = Vehicles.Where(v => listOfStrings.Contains(v.Name));

0

Score: 0

You can perform an Inner Join:

var matchingVehicles = from vehicle in vehicles
                       join item in listOfStrings on vehicle.Name equals item
                       select vehicle;

0

Score: 0
Vehicles.Where(vehicle => listOfStrings.Contains(vehicle.Name))

0

Score: 0

To check if a string contains one of these 1 characters (Boolean output):

var str= "string to test";
var chr= new HashSet<char>{',', '&', '.', '`', '*', '$', '@', '?', '!', '-', '_'};
bool test = str.Any(c => chr.Contains(c));

More Related questions