Reputation: 6471
I have this:
Dictionary<integer, string> dict = new Dictionary<integer, string>();
I want to select all the items in the dictionary that contain the value abc
.
Is there an inbuilt function that lets me do this easily?
Upvotes: 20
Views: 37601
Reputation: 11387
Built-in function? No sorry... but another (not so beautiful) way is to iterate using foreach(KeyValuePair<integer, string> ...
Upvotes: 0
Reputation: 1499840
Well it's reasonably simple with LINQ:
var matches = dict.Where(pair => pair.Value == "abc")
.Select(pair => pair.Key);
Note that this won't be even slightly efficient - it's an O(N)
operation, as it needs to check every entry.
If you need to do this frequently, you may want to consider using another data structure - Dictionary<,>
is specifically designed for fast lookups by key.
Upvotes: 55