代码之家  ›  专栏  ›  技术社区  ›  Retrocoder

将列表列表强制转换为iGroup?

  •  1
  • Retrocoder  · 技术社区  · 14 年前

     List<List<Device>> GroupedDeviceList = new List<List<Device>>();
    

     IEnumerable<IGrouping<object, Device>>
    

    这是可能的,通过一个演员等,还是我应该使用一个不同的定义我的名单?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Grozz    14 年前

    如果我正确理解你的意图,答案如下:

        var result = GroupedDeviceList
            .SelectMany(lst => lst.GroupBy(device => (object)lst));
    

    结果的类型为 IEnumerable<IGrouping<object, Device>> ,其中object是对 List<Device>

        2
  •  1
  •   Winston Smith    14 年前

    IGrouping<T1,T2> 通常通过LINQ创建 GroupBy

    你是怎么填充的 GroupedDeviceList .GroupBy ?

    您可以通过执行以下操作再次执行分组:

    // IEnumerable<IGrouping<TWhatever, Device>>
    var reGroupedList = 
        GroupedDeviceList
        .SelectMany( innerList => innerList.GroupBy( device => device.Whatever ) );
    
        3
  •  1
  •   Amy B    14 年前

    假设每个非空列表的内容应该组成一个组。。。

    IEnumerable<IGrouping<object, Device>> query =
      from list in lists
      from device in list
      group device by (object)list;