Dilshod
Dilshod

Reputation: 3311

Check if following items exist in the List<T>

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!

Upvotes: 8

Views: 19285

Answers (2)

Rwiti
Rwiti

Reputation: 1066

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));

Upvotes: 3

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

var valuesToCheck = new[] {"one", "three"};
bool isAllInList = valuesToCheck.All(s => strings.Contains(s));

Upvotes: 19

Related Questions