Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better GroupBy to Dictionary (or solution) to bucketting?

Is there a way to write the ToDictionary statement below using the SQL-ish Linq syntax?

public class KeyedType
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Dictionary<string,List<KeyedType>> groups =
    list.GroupBy((g) => g.Name)
        .ToDictionary(g => g.Key, g => g.ToList());
like image 314
lucidquiet Avatar asked Nov 17 '11 17:11

lucidquiet


1 Answers

Whenever you find yourself with a Dictionary<TKey, List<TSomething>>, you may find you can happily use a Lookup<TKey, TSomething>. If this proves to be the case, you can use ToLookup to make one.

However, neither for ToLookup nor for your code is there a query expression syntax available, unfortunately.

like image 165
AakashM Avatar answered Oct 17 '22 14:10

AakashM