[ACCEPTED]-How to determine if a string contains any matches of a list of strings-linq
Accepted answer
Vehicles.Where(v => listOfStrings.Contains(v.Name))
0
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));
would this work:
listOfStrings.Contains("Trucks");
0
var m = Vehicles.Where(v => listOfStrings.Contains(v.Name));
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
Vehicles.Where(vehicle => listOfStrings.Contains(vehicle.Name))
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));
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.