我的问题是关于使用 C# linq 比较 List<string> 和 Dictionary <string, List<string>>

Nac*_*n R 3 c# linq dictionary list

我已经根据我的要求在堆栈溢出中引用了以下链接,该链接提供匹配的键和布尔结果。我需要字典中与字符串列表匹配的键和值的结果。

参考下面的链接: Array with Dictionary in c# using Linq

该示例在上面的同一链接中提供。我将如何在下面提供相同的内容

Dictionary<int, List<string>> dict = new Dictionary<int, <string>>
{ 
    {1, new List<string>(){"A","B"}},
    {2, new List<string>(){"C","D"}},
    {3, new List<string>(){"G","H"}},
    {4, new List<string>(){"E","F"}},
    {5, new List<string>(){"I","J"}},
};

string[] values = new [] 
{
    "A", "D", "E"
};

var result = 
  from kvp in dict
  join s in values on kvp.Value equals s
select new {kvp.Key, Found = true};
Run Code Online (Sandbox Code Playgroud)

我试过的如下:

var result = dict
  .Select(x => new { 
     keys = x.Key, 
     values = values
       .ToList()
       .Any(x.Value.Contains) 
   });
Run Code Online (Sandbox Code Playgroud)

我期望输出就像{1,A},{2,D},{4,E}但实际上来了{1,True},{4,True},{5,True}

有人可以帮我解决这个问题。

Dmi*_*nko 5

从技术上讲,如果你愿意,说, List<object> == {1, "A", 4, "D", 5, "E"};

我希望输出像 {1,A,4,D,5,E}

您可以尝试Where过滤掉dict记录并将SelectMany它们展平:

  Dictionary<int, string> dict = new Dictionary<int, string>() { 
    {1, new "A"},
    {2, new "B"},
    {3, new "c"},
    {4, new "D"},
    {5, new "E"},
  };

  string[] values = new [] {"A", "D", "E"};

  var result = dict
    .Where(pair => values.Contains(pair.Value))
    .SelectMany(pair => new object[] { pair.Key, pair.Value})
    .ToList();

  Console.Write(string.Join(", ", result));
Run Code Online (Sandbox Code Playgroud)

结果:

  1, A, 4, D, 5, E
Run Code Online (Sandbox Code Playgroud)

但是,我怀疑您是否真的想要这种奇怪的数据表示。甲过滤收集(或字典),而不 SelectMany是更方便的:

  List<KeyValuePair<int, string>> result = dict
    .Where(pair => values.Contains(pair.Value))
    .ToList();

  Console.Write(string.Join(", ", result.Select(pair => $"{pair.Key}, {pair.Value}")));
Run Code Online (Sandbox Code Playgroud)

或者

  Dictionary<int, string> result = dict
    .Where(pair => values.Contains(pair.Value))
    .ToDictionary(pair => pair.Key, pair => pair.value);

  Console.Write(string.Join(", ", result.Select(pair => $"{pair.Key}, {pair.Value}")));
Run Code Online (Sandbox Code Playgroud)

产生相同的结果:

  1, A, 4, D, 5, E
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,问题发生了巨大变化。现在我们有

  Dictionary<int, List<string>> dict = new Dictionary<int, <string>>() { 
    {1, new List<string>() {"A", "B"}},
    {2, new List<string>() {"C", "D"}},
    {3, new List<string>() {"G", "H"}},
    {4, new List<string>() {"E", "F"}},
    {5, new List<string>() {"I", "J"}},
  };

  string[] values = new [] { "A", "D", "E" };
Run Code Online (Sandbox Code Playgroud)

我们想要过滤Dictionary<int, List<string>>

  var result = dict
    .Select(pair => new {
      key = pair.Key,
      value = pair.Value.Intersect(values).ToList()
    })
    .Where(item => item.value.Any())
    .ToDictionary(item => item.key, item => item.value);

  Console.Write(string.Join(Environment.NewLine, result
    .Select(pair => $"{pair.Key} : [{string.Join(", ", pair.Value)}]"))); 
Run Code Online (Sandbox Code Playgroud)

结果:

1 : [A]
2 : [D]
4 : [E]   
Run Code Online (Sandbox Code Playgroud)