简体   繁体   English

将LINQ查询转换为字典<string, string[]>

[英]Converting a LINQ query into a Dictionary<string, string[]>

I've got a query that returns something of the following format: 我有一个返回以下格式的查询:

{ "tesla", "model s" }
{ "tesla", "roadster" }
{ "honda", "civic" }
{ "honda", "accord" }

and I'd like to convert that to a dictionary of <string, string[]> like so: 我想将它转换为<string, string[]>的字典,如下所示:

{ "tesla" : ["model s", "roadster"],  "honda" : ["civic", "accord"] }

I've tried with this: 我试过这个:

var result = query.Select(q => new { q.Manufacturer, q.Car}).Distinct().ToDictionary(q => q.Manufacturer.ToString(), q => q.Car.ToArray()); 

but so far I am not having any luck. 但到目前为止我没有运气。 I think what this is doing is actually trying to add individual items like "tesla" : ["model s"] and "tesla" : ["roadster"] and that's why it's failing ... any easy way to accomplish what I am trying to do in LINQ? 我认为这样做实际上是在试图添加像"tesla" : ["model s"]这样的单个项目"tesla" : ["model s"]"tesla" : ["roadster"]这就是它失败的原因......任何简单的方法来实现我的目标想在LINQ做什么?

You would need to group each item by the key first, then construct the dictionary: 您需要先按键对每个项目进行分组,然后构造字典:

result = query.Select(q => new { q.Manufacturer, q.Car}).Distinct()
              .GroupBy(q => q.Manufacturer)
              .ToDictionary(g => g.Key, 
                            g => g.Select(q => q.Car).ToArray());

Of course, an ILookup<string, string> much easier: 当然, ILookup<string, string>更容易:

result = query.Select(q => new { q.Manufacturer, q.Car }).Distinct()
              .ToLookup(q => q.Manufacturer, q => q.Car);

You're looking for ToLookup if you would like the results to be grouped into a dictionary-like object: 如果您希望将结果分组为类似字典的对象,那么您正在寻找ToLookup

var result = query.Select(q => new { q.Manufacturer, q.Car})
                  .Distinct()
                  .ToLookup(q => q.Manufacturer.ToString(), q => q.Car);

Otherwise you will have to group the results first: 否则,您必须先对结果进行分组:

var result = query.Select(q => new { q.Manufacturer, q.Car })
                  .Distinct()
                  .GroupBy(q => q.Manufacturer)
                  .ToDictionary(gg => gg.Key,
                                gg => gg.Select(q => q.Car).ToArray());

What you want is GroupBy() , followed by ToDictionary() . 你想要的是GroupBy() ,然后是ToDictionary()

Example: 例:

var result = query.GroupBy(q => q.Manufacturer).ToDictionary(q => q.Key, q => q.Value.ToArray());

What GroupBy() does is group all the elements that have the same matching key selector. GroupBy()所做的是对具有相同匹配键选择器的所有元素进行分组。 So when you tell it to GroupBy(q => q.Manufacturer) , all the elements that have the same Manufacturer will be grouped together as IEnumerable<T> . 因此,当您将其告知GroupBy(q => q.Manufacturer) ,具有相同制造商的所有元素将组合在一起作为IEnumerable<T>

Use ToLookup : 使用ToLookup

var table = pairs.ToLookup(kvp => kvp.Key, kvp => kvp.Value);

foreach(var i in table["tesla"])
   Console.WriteLine(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM