Dictionary search with Linq
we can search dictionary like
var dictionary = new Dictionary<string,string>();
dictionary.Keys.Where( key => key.Contains("a")).ToList();
but it return list. i want that linq should 开发者_如何学编程return true or false. so what would be the right code that search dictionary with linq. please guide.
Use the Any()
operator:
dictionary.Keys.Where(key => key.Contains("a")).Any();
Or
dictionary.Keys.Any(key => key.Contains("a"));
Use Any
instead of Where
:
dictionary.Keys.Any( key => key.Contains("a"));
You can use the .Any() keyword:
bool exists = dictionary.Keys.Any(key => key.Contains("a"));
If you're asking if you can determine whether or not any key in the dictionary contains "a"
, then you can do:
dictionary.Keys.Any(key => key.Contains("a"))
精彩评论