gpt4 book ai didi

c# - 如何在 C# 中使用 LINQ 从字典中删除项目

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:27 24 4
gpt4 key购买 nike

我需要从字典中删除特定项目..

字典是这样的

      dict["Key1"]="Value1"
dict["Key2"]="Value2"
dict["Key3"]="Value3"
dict["Key4"]="Value2"

如果另一个项目具有相同的值,如何使用 LINQ 删除该项目

提前致谢

最佳答案

这里是我测试过的解决方案:

dict.GroupBy(x => x.Value, x => x.Key)
.Where(x => x.Count() > 1)
.SelectMany(x => x.Skip(1))
.ToList().ForEach(x => dict.Remove(x))

关于c# - 如何在 C# 中使用 LINQ 从字典中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3146907/

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