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

我只想选择值大于 5 的键。我该怎么做?

4

1 回答 1

5

只需选择条目,根据值进行过滤,然后投影到键:

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

请注意,这种方法对任何人都适用IDictionary<,>- 您拥有 a 的事实在ConcurrentDictionary<,>这里无关紧要。

于 2015-01-29T18:46:27.603 回答