gpt4 book ai didi

c# - 使用 LINQ 表达式获取多个嵌套字典除外

转载 作者:行者123 更新时间:2023-12-03 21:25:53 26 4
gpt4 key购买 nike

我想使用 lambda 获得 n 个不同的字典表达:

Dictionary<string, string> d1 = new Dictionary<string, string>();
d1.Add("Joe", "2, Barfield Way");
d1.Add("Mike", "17, Apollo Avenue");
d1.Add("Jane", "69, Lance Drive");


Dictionary<string, string> d2 = new Dictionary<string, string>();
d2.Add("Joe", "2, Barfield Way");
d2.Add("Jane", "69, Lance Drive");
// var diff = d1.Except(d2);

假设我想获得上述两个词典的差异 var diff = d1.Except(d2);
现在我想使用 N 的 lambda 表达式得到相同的结果字典的数量。

一瞬间,我将两本词典合二为一。我想使用 lambda 得到两个字典的差异表达式或任何其他 LINQ表达。
Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>();
d.Add("Test", d1);
d.Add("Test2", d2);

我尝试了下面的表达式,但没有得到任何结果。
d.Select(c => c.Value.Except(c.Value))

最佳答案

您需要一些 Linq 方法:

var result = d.SelectMany(d => d.Value).GroupBy(c => c.Key)
.Where(c => c.Count() == 1).ToDictionary(t => t.Key, t => t.Select(c => c.Value)
.FirstOrDefault()).ToList();

关于c# - 使用 LINQ 表达式获取多个嵌套字典除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59677508/

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