how to retrieve keys only when filter by value

ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>();
dic.AddOrUpdate(1, 2, (s, i) => 0);
dic.AddOrUpdate(2, 3, (s, i) => 0);
dic.AddOrUpdate(3, 1, (s, i) => 0);
dic.AddOrUpdate(4, 7, (s, i) => 0);

I want to only select keys where the value is greater than 5. How can I do this?

Jon Skeet
people
quotationmark

Just select the entries, filter based on the values, then project to the keys:

var keys = dic.Where(entry => entry.Value > 5)
              .Select(entry => entry.Key);

Note that this approach is fine for any IDictionary<,> - the fact that you've got a ConcurrentDictionary<,> is irrelevant here.

people

See more on this question at Stackoverflow