ListCollectionView分组已经分组的项目

时间:2016-08-15 11:52:22

标签: c# wpf mvvm

如何在ListCollectionView中进行双重分组(通过双重分组,我的意思不仅仅是按两个属性进行分组,而是将已经按其他属性分组的内容进行分组)?

示例:

class Person{
     public string Name;
     public int Age;
{

List<Person> list = new List<Person>{
  new Person{ Name = Alex, Age = 22 },
  new Person{ Name = Alex, Age = 23 },
  new Person{ Name = Sam, Age = 19 },
  new Person{ Name = Sam, Age = 33 }
};
ListCollectionView listView = new ListCollectionView(list);
listView.GroupDescriptions.add(new PropertyGroupDescription("Name"));

that is all i have right now, is there some way?

亚历:

22

Alex 22;

23

Alex 23;

山姆:

19

Sam 19;

33

Sam 33;

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

var result= list.GroupBy(item => item.Name)
                .Select(group => new 
                { 
                    Name = group.Key,
                    Values = group.GroupBy(item => item.Age)
                                  .Select(innerGroup => new 
                                  { 
                                      Age = group.Key, 
                                      Values = group.ToList() 
                                  }).ToList()
                }).ToList();