使用LINQ从字典<string,List <string >>中删除重复值

sst*_*sst 1 .net c# linq dictionary

我有一个包含string键和List<string>值的字典.我想为每个列表获取唯一值.

例如,这个输入数据:

{
    "first": ["1", "2", "3", "1"],
    "second": ["2", "3", "4", "3"]
}
Run Code Online (Sandbox Code Playgroud)

会回来这个:

{
    "first": ["1", "2", "3"],
    "second": ["2", "3", "4"]
}
Run Code Online (Sandbox Code Playgroud)

我试过这个,但它不起作用:

var uniq = duplidictionary.GroupBy(x => x.Value)
                          .Select(y => y.First())
                          .ToDictionary( x => x.Key, y => y.Value);
Run Code Online (Sandbox Code Playgroud)

这似乎返回了原始字典的副本,而不是返回我预期的结果.这是一个DotNetFiddle,说明我的代码不起作用.

如何使用LINQ从给定输入获取此输出?

Mat*_*gen 5

我想你需要这样的东西:

Dictionary<string, List<string>> dupeLists = ...;

var ret = dupeLists.ToDictionary(c => c.Key, c => c.Value.Distinct().ToList());
Run Code Online (Sandbox Code Playgroud)

这会将项目复制到新词典中,只对值列表中的每个项目进行一次复制.

所以如果dupeLists看起来像:

{
    "first": ["1", "2", "3", "1"],
    "second": ["2", "3", "4", "3"]
}
Run Code Online (Sandbox Code Playgroud)

然后这将返回:

{
    "first": ["1", "2", "3"],
    "second": ["2", "3", "4"]
}
Run Code Online (Sandbox Code Playgroud)

你的代码没有工作,因为它是寻找独特的名单相比,这些名单中唯一的值.因为每个列表在内存中是不同的位置(您知道因为修改一个不会修改另一个),所以您的GroupBy调用产生的每个组只有一个元素长.


最初的问题远没有现在那么明确,所以我提出了几个变体来确保找到正确的答案.下面列出的是后人,但结果却不适用于这个特殊情况.


只是为了好的衡量,你说你需要 "摆脱重复的价值观",这是模棱两可的.如果你想抛弃任何有重复的东西,

Dictionary<string, List<string>> dupeLists = ...;

var ret = dupeLists.ToDictionary(c => c.Key, c => c.Value.GroupBy(x => x)
                                                         .Where(x => x.Count() == 1)
                                                         .Select(x => x.Key)
                                                         .ToList());
Run Code Online (Sandbox Code Playgroud)

将返回:

{
    "first": ["2", "3"],
    "second": ["2", "4"]
}
Run Code Online (Sandbox Code Playgroud)

因为你在一个沮丧的日子抓住我,如果你真的想要返回一个不同项目的平面列表,

Dictionary<string, List<string>> dupeLists = ...;

var ret = dupeLists.SelectMany(c => c.Value).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

产量:

["1", "2", "3", "4"]
Run Code Online (Sandbox Code Playgroud)

或者只是在整个字典中只出现一次的那些:

Dictionary<string, List<string>> dupeLists = ...;

var ret = dupeLists
              .SelectMany(c => c.Value)
              .GroupBy(c => c)
              .Where(c => c.Count() == 1)
              .Select(c => c.Key)
              .ToList();
Run Code Online (Sandbox Code Playgroud)

这是:

["4"]
Run Code Online (Sandbox Code Playgroud)

或者只是那些只出现在任何给定列表中但没有出现在其中的列表:

Dictionary<string, List<string>> dupeLists = ...;

var ret = dupeLists
              .SelectMany(c => c.Value, (kvp, Value) => new { kvp.Key, Value })
              .GroupBy(c => c.Value)
              .Where(c => c.Select(x => x.Key).Distinct().Count() == 1)
              .GroupBy(c => c.Key, c => c.Value)
              .ToDictionary(c => c.Key, c => c.ToList());
Run Code Online (Sandbox Code Playgroud)

也就是说,如果我未经测试的代码保持不变:

{
    "first": ["1", "1"],
    "second": ["4"]
}
Run Code Online (Sandbox Code Playgroud)