简体   繁体   English

推荐的转换IGrouping的方法 <TKey, TValue> 到IDictionary <TKey, IEnumerable<TValue> &gt;

[英]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.

相关问题 如何转换IDictionary <TKey, List<TValue> &gt;到IDictionary <TKey, IEnumerable<TValue> &gt;? - How to convert IDictionary<TKey, List<TValue> > to IDictionary<TKey, IEnumerable<TValue> >? 转换iDictionary <TKey,TValue> 进入IDictionary <TSomeProperty, ICollection<TValue> &gt; - Convert iDictionary<TKey,TValue> into IDictionary<TSomeProperty, ICollection<TValue>> 嵌套接口:演员表IDictionary <TKey, IList<TValue> &gt;到IDictionary <TKey, IEnumerable<TValue> &gt;? - Nested Interfaces: Cast IDictionary<TKey, IList<TValue>> to IDictionary<TKey, IEnumerable<TValue>>? ObservableDictionary类 <TKey, TValue> :IDictionary <TKey, TValue> 作为DataContract - class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue> as DataContract 为什么IDictionary <TKey,TValue>实现了ICollection和IEnumerable - Why IDictionary<TKey, TValue> implements both ICollection and IEnumerable 为什么IDictionary <TKey,TValue>扩展ICollection <KeyValuePair <TKey,TValue >>? - Why does IDictionary<TKey, TValue> extend ICollection<KeyValuePair<TKey, TValue>>? 为什么要向上转换IDictionary <TKey, TValue> 到IEnumerable <object> 失败? - Why does upcasting IDictionary<TKey, TValue> to IEnumerable<object> fail? 为什么反射器不显示 IDictionary<TKey, TValue> 实现 IEnumerable<T> ? - Why does the reflector not show that IDictionary<TKey, TValue> implements IEnumerable<T>? IEnumerable的 <T> 。去查查看 <TKey, TValue> - IEnumerable<T>.ToLookup<TKey, TValue> IDictionary <TKey,TValue>替换值的实现和合同 - IDictionary<TKey, TValue> implementation and contract for replacing values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM