user610217
user610217

Reputation:

Recommended way to convert IGrouping<TKey, TValue> to IDictionary<TKey, IEnumerable<TValue>>

This has got to be a duplicate, but my searching is not yielding the results I want.

It seems this should be fairly straightforward, but no built-in LINQ mechanism seems to be in place to make this happen. Some help would be appreciated.

...it's also possible I'm doing this wrong. I have an enumerable set of objects with a property Foo that I want to create a dictionary with the Foo property the key of a dictionary, where the value is an enumeration of objects having Foo as the same value.

Upvotes: 1

Views: 1300

Answers (1)

King King
King King

Reputation: 63317

This should work:

var dict = yourGroup.ToDictionary(g=>g.Key, g=>g.Select(x=>x));

Or use AsEnumerable for generating values:

var dict = yourGroup.ToDictionary(g=>g.Key, g=>g.AsEnumerable());

Upvotes: 6

Related Questions