【问题标题】:Is there a way to flatten a .Net ILookup<TKey, TElement> into a List<TElement>?有没有办法将 .Net ILookup<TKey, TElement> 展平为 List<TElement>?
【发布时间】:2010-02-24 02:59:12
【问题描述】:

有没有一种快速的方法可以从通过IEnumerable&lt;TElement&gt; 扩展创建的ILookup&lt;TKey, TElement&gt; 获得扁平化的List&lt;TElement&gt;


更新示例

List<int> list = new List<int>();
var lookup = list.ToLookup(key => key);
list = lookup.?? // How to convert the lookup back to the list

【问题讨论】:

    标签: .net ilookup


    【解决方案1】:
    lookup.SelectMany( x => x ).ToList()
    

    不过,转换为 ILookup 并再次返回很可能会改变顺序。

    【讨论】:

      【解决方案2】:

      不确定这是否是您所追求的。对于Dictionary&lt;&gt;List&lt;&gt;

      List<TElement> list iLookUp.Values.ToList<TElement>();
      

      List&lt;&gt;Dictionary&lt;&gt;

      var dict = list.Cast<TElement>().ToDictionary(t => t.Id, t => t.Description);
      

      【讨论】:

      • 谢谢,虽然不是我想要的。为了清楚起见,我已经更新了问题
      • 看起来ILookup 没有Values 属性。
      猜你喜欢
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多