[英]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. 看来这应该相当简单,但是似乎没有内置的LINQ机制可以实现这一目标。 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. 我有一个具有
Foo
属性的可枚举对象集,我想用Foo
属性创建一个字典的字典,该字典的键值是具有相同Foo
值的对象的枚举。
This should work: 这应该工作:
var dict = yourGroup.ToDictionary(g=>g.Key, g=>g.Select(x=>x));
Or use AsEnumerable
for generating values: 或使用
AsEnumerable
生成值:
var dict = yourGroup.ToDictionary(g=>g.Key, g=>g.AsEnumerable());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.