Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if following items exist in the List<T>

Tags:

c#

linq

I have a list of strings and I need to check if specific items(not one item) exist in that list.

List<string> strings = new List<string>() {"one","two","three","four","five" };

I need to find out if "one" and "three" is in that list. Is it possible with one linq query?

Thanks for the help!

like image 482
Dilshod Avatar asked Jul 12 '13 18:07

Dilshod


Video Answer


2 Answers

var valuesToCheck = new[] {"one", "three"};
bool isAllInList = valuesToCheck.All(s => strings.Contains(s));
like image 183
Sergey Berezovskiy Avatar answered Oct 24 '22 07:10

Sergey Berezovskiy


var findMe = new List<string>() { "one", "three"};
List<string> strings = new List<string>() { "one", "two", "three", "four", "five" };

var result = findMe.All(f => strings.Any(s => f == s));
like image 25
Rwiti Avatar answered Oct 24 '22 08:10

Rwiti