2016-08-15 34 views
1

我如何在ListCollectionView中進行雙重分組(通過雙重分組,我的意思是不僅僅是由兩個屬性分組,而是根據另一個屬性分組的內容)?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:

亞歷22;

23:

亞歷23;

山姆:

19:

薩姆19;

33:

薩姆33;

在此先感謝您的幫助!

回答

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(); 
+0

可以在分組後使用ListCollectionView中的結果嗎?因爲我需要將它綁定到ListBox – kotki