C# : 如何比较字典值?[英] C# : How to compare Dictionary Values?

本文是小编为大家收集整理的关于C# : 如何比较字典值?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个Dictionary<String,String>如下

 Dictionary<String, String> MyDict = new Dictionary<String, String>();

其中包含

 MyDict.Add("A", "1010");
 MyDict.Add("B", "1011");
 MyDict.Add("C", "1110");
 MyDict.Add("D", "1010");
 MyDict.Add("E", "1011");
 MyDict.Add("F", "1010");

我需要比较字典 Values 然后添加所有具有相同值的 keys

这是我的结果字典

 Dictionary<String, List<String>> MyResultDict = new Dictionary<String, List<String>>();

还有我的代码

 var XXX = MyDict.ToLookup(X => X.Value, X => X.Key);
 MyResultDict = MyDict.ToDictionary(X => X.Key, X => XXX[X.Value].ToList());

上面的代码会产生类似的结果

 { "A"     { "A" , "D", "F" } }
 { "B"     { "B" , "E" } }
 { "C"     { "C" } }
 { "D"     { "A" , "D", "F" } }
 { "E"     { "B" , "E" } }
 { "F"     { "A" , "D", "F" } }

但我的解决方案有两个问题

  1. 存在重复值列表(对于键 D 、 E 和 F.)

  2. 值列表包含键.

预期输出是这样的

 { "A"     { "D", "F" } }
 { "B"     { "E" } }

即不需要key C,因为C的值不会在任何地方重复.

并且没有必要包含 keys D , E and F,因为它已经包含在 A 和 B 的值列表中.

如何使用 Linq 或 Lambda Expression 做到这一点?

推荐答案

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Select(grp => new { Key = grp.First().Key, Matches = grp.Skip(1).Select(k => k.Key).ToList() })
    .Where(m => m.Matches.Count > 0)
    .ToDictionary(m => m.Key, m => m.Matches);

或者可能更简单一点:

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Where(grp => grp.Count() > 1)
    .ToDictionary(grp => grp.First().Key, grp => grp.Skip(1).Select(k => k.Key).ToList());

本文地址:https://www.itbaoku.cn/post/1556846.html

问题描述

I have a Dictionary<String,String> as follows

 Dictionary<String, String> MyDict = new Dictionary<String, String>();

and which contains

 MyDict.Add("A", "1010");
 MyDict.Add("B", "1011");
 MyDict.Add("C", "1110");
 MyDict.Add("D", "1010");
 MyDict.Add("E", "1011");
 MyDict.Add("F", "1010");

I need to Compare the Dictionary Values and then to add the keys which all having same values

here is My resulting Dictionary

 Dictionary<String, List<String>> MyResultDict = new Dictionary<String, List<String>>();

And My code

 var XXX = MyDict.ToLookup(X => X.Value, X => X.Key);
 MyResultDict = MyDict.ToDictionary(X => X.Key, X => XXX[X.Value].ToList());

The above code will produce the result like

 { "A"     { "A" , "D", "F" } }
 { "B"     { "B" , "E" } }
 { "C"     { "C" } }
 { "D"     { "A" , "D", "F" } }
 { "E"     { "B" , "E" } }
 { "F"     { "A" , "D", "F" } }

But my solution is having two problem

  1. There exist Duplicate value list ( for the keys D , E and F.)

  2. The Value List includes the key.

The Expected OutPut is like

 { "A"     { "D", "F" } }
 { "B"     { "E" } }

i.e There is no need of key C because C's value is not repeated anywhere.

and there is no need to include keys D , E and F because its already included in A's and B's value lists.

How to do this using Linq or Lambda Expression?

推荐答案

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Select(grp => new { Key = grp.First().Key, Matches = grp.Skip(1).Select(k => k.Key).ToList() })
    .Where(m => m.Matches.Count > 0)
    .ToDictionary(m => m.Key, m => m.Matches);

or possibly a bit simpler:

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Where(grp => grp.Count() > 1)
    .ToDictionary(grp => grp.First().Key, grp => grp.Skip(1).Select(k => k.Key).ToList());