gpt4 book ai didi

c# - 如何仅在按值过滤时检索键

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:05 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com