【发布时间】:2018-03-24 14:29:23
【问题描述】:
假设我有以下对象
public class DepartmentSchema
{
public int Parent { get; set; }
public int Child { get; set; }
}
我有一个List<DepartmentSchema>,结果如下:
Parent | Child
---------------
4 | 1
8 | 4
5 | 7
4 | 2
8 | 4
4 | 1
我想将所有具有相同父值和子值的对象分组 分组后我想要的结果是以下列表
Parent | Child
---------------
4 | 1
8 | 4
5 | 7
4 | 2
我成功使用 IGrouping =>
departmentSchema.GroupBy(x => new { x.Parent, x.Child }).ToList();
但结果是List<IGrouping<'a,DepartmentSchema>> 而不是List<DepartmentSchema>
我知道我可以创建一个新的 foreach 循环并从组列表中创建一个新的List<DepartmentSchema>,但我想知道是否有更好的方法
提前致谢
【问题讨论】: