如何仅在按值过滤时检索键
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);
我只想 select 个值大于 5 的键。我该怎么做?
只是 select 个条目,根据值进行过滤,然后投射到键:
var keys = dic.Where(entry => entry.Value > 5)
.Select(entry => entry.Key);
请注意,此方法适用于任何 IDictionary<,>
- 您拥有 ConcurrentDictionary<,>
的事实与此无关。
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);
我只想 select 个值大于 5 的键。我该怎么做?
只是 select 个条目,根据值进行过滤,然后投射到键:
var keys = dic.Where(entry => entry.Value > 5)
.Select(entry => entry.Key);
请注意,此方法适用于任何 IDictionary<,>
- 您拥有 ConcurrentDictionary<,>
的事实与此无关。