Dictionary Search with Linq

we can search for a dictionary, for example

var dictionary = new Dictionary<string,string>(); dictionary.Keys.Where( key => key.Contains("a")).ToList(); 

but it returns a list. I want linq to return true or false. so that will be the correct code to search for a dictionary with linq. please guide.

+6
source share
4 answers

Use the Any() operator:

 dictionary.Keys.Where(key => key.Contains("a")).Any(); 

or

 dictionary.Keys.Any(key => key.Contains("a")); 
+16
source

Use Any instead of Where :

 dictionary.Keys.Any( key => key.Contains("a")); 
+7
source

You can use the .Any () keyword:

 bool exists = dictionary.Keys.Any(key => key.Contains("a")); 
+2
source

If you ask if you can determine if any key contains a in the dictionary, you can do:

 dictionary.Keys.Any(key => key.Contains("a")) 
+1
source

Source: https://habr.com/ru/post/896232/


All Articles