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的键。我该怎么做?
最佳答案
只需选择条目,基于值进行过滤,然后投影到键:
var keys = dic.Where(entry => entry.Value > 5)
.Select(entry => entry.Key);
请注意,这种方法对任何
IDictionary<,>
都适用-在这里,您没有ConcurrentDictionary<,>
的事实是无关紧要的。关于c# - 仅在按值过滤时如何检索键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28222222/