score:3

Given an IEnumerable<ContainerClass> named objects, you could do this:

var dict = objects
    .ToLookup(o => o.Group)
    .ToDictionary(grp => grp.Key, grp => grp.Select(o => o.Value).ToList());

Explanation:

ToLookup method converts the IEnumerable<ContainerClass> to an ILookup<Guid, ContainerClass>. The ToDictionary method then takes the collection of IGrouping<Guid, ContainerClass> objects, and selects the Key of the grouping as the key for the dictionary, and then selects the Value property of the ContainerClass objects, and converts that to a List<int> for the dictionary values.

You may not actually need a Dictionary<ContainerClass>. If you're OK with using an ILookup<Guid, int> instead, you can just do this:

var lookup = objects.ToLookup(o => o.Group, o => o.Value);

score:7

Rather than a Dictionary<Guid, List<int>> could you use a Lookup<Guid, int>? That would make it easy:

var lookup = list.ToLookup(x => x.Group, x => x.Value);

A Lookup is much like a Dictionary, but designed to work with multiple values per key. On interesting difference is that if you fetch a missing key, you'll just get an empty sequence back instead of an exception.

If you can use that, that's great. Otherwise you could always convert from a Lookup to a Dictionary:

var dictionary = list.ToLookup(x => x.Group, x => x.Value)
                     .ToDictionary(x => x.Key, x => x.Value.ToList());

score:4

I'm assuming you have multiple instances of the class, otherwise it is trivial. In LINQ, something similar is:

var lookup = data.ToLookup(obj => obj.Group, obj => obj.Value);

Then you can, for example:

foreach(var value in lookup[someGuid]) {
    ...
}

If you really need this as a dictionary (rather than a lookup, which is close):

var lookup = data.GroupBy(obj => obj.Group).ToDictionary(
          grp => grp.Key, grp => grp.Select(obj => obj.Value).ToList());

Related Articles