Brian D
Brian D

Reputation: 10163

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:

{ "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?

Upvotes: 5

Views: 1161

Answers (4)

user7116
user7116

Reputation: 64148

You're looking for ToLookup if you would like the results to be grouped into a dictionary-like object:

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

Upvotes: 3

p.s.w.g
p.s.w.g

Reputation: 149078

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:

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

Upvotes: 3

sehe
sehe

Reputation: 393934

Use ToLookup:

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

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

Upvotes: 0

rossipedia
rossipedia

Reputation: 59437

What you want is GroupBy(), followed by 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. 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>.

Upvotes: 1

Related Questions