使用LINQ表达式获取多个嵌套字典的例外。

5

我想要使用 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);

现在我想使用lambda表达式来获取N个字典的相同输出。

例如,我将两个字典合并为一个。我想使用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))

如果您觉得某个答案解决了您的问题,请不要忘记通过点击答案旁边的灰色勾选标记将其标记为已接受的答案。https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 - Salah Akbari
4个回答

4

您需要一些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();

@MarcoSalerno 这个可以很容易地改为 c => new { c.Key, c.Value },但我怀疑单列分组或多列分组是 OP 所寻找的,也许我们需要更多来自他方面的澄清。顺便说一下,这个链接可能是多列分组的一个很好的参考 https://dev59.com/7XI-5IYBdhLWcg3w8NOB#38434457 - Salah Akbari

1
将其转换为一组KeyValuePair<>的枚举,并使用.Aggregate()遵循相同的逻辑。
var result = d.Select(x => x.Value.AsEnumerable()).Aggregate((x, y) => x.Except(y));

0

这样做将会得到相同的结果:

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);

Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>();
d.Add("Test", d1);
d.Add("Test2", d2);

var diff1 = d.SelectMany(x => x.Value).GroupBy(x => new { x.Key, x.Value }).Where(x => x.Count() == 1).SelectMany(x => x.AsEnumerable());

0

您可以将字典存储在 List<Dictionary<string, string>> 中。

然后查询此集合并按键分组,通过键的计数进行过滤以仅获取唯一的键,然后构建一个新的字典:

var d1 = new Dictionary<string, string>
{
    { "Joe", "2, Barfield Way" },
    { "Mike", "17, Apollo Avenue" },
    { "Jane", "69, Lance Drive" }
};

var d2 = new Dictionary<string, string>
{
    { "Joe", "2, Barfield Way" },
    { "foo", "bar" },
    { "Jane", "69, Lance Drive" }
};

var d3 = new Dictionary<string, string>
{
    { "hello", "world" },
    { "foo", "bar" }
};

var dicts = new List<Dictionary<string, string>>
{
    d1,
    d2,
    d3
};

var distinct = dicts.SelectMany(d => d)          // Flatten the collection of dictionaries
                    .GroupBy(d => d.Key)         // Group the sequence by key
                    .Where(d => d.Count() == 1)  // Filter the result for unique keys only
                    .ToDictionary(k => k.Key, v => v.Select(x => x.Value)
                                                    .First()); // Materialize the sequence in a Dictionary<string, string>

foreach (var key in distinct.Keys)
{
    Console.WriteLine($"{key} -> {distinct[key]}");
}

输出为

Mike -> 17, Apollo Avenue
hello -> world

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接