2015-10-07 160 views
1

我有一個對象列表,其中一個項目是另一個列表。我如何根據內部列表對它們進行分組。 這是我想要做的一個例子。按另一個列表順序列表排序

class Student 
{ 
    public string Name; 
    public int Age; 
    public List<GroupInfo> GroupList; // This is the inner list 
} 

class GroupInfo 
{ 
    public string GroupName; 
    public int GroupId; 
} 

static void Main() 
{ 
    GroupInfo firstGroup = new GroupInfo 
    { 
     GroupId = 1, 
     GroupName = "First group" 
    }; 

    GroupInfo secondGroup = new GroupInfo 
    { 
     GroupId = 2, 
     GroupName = "Second group" 
    }; 

    GroupInfo thirdGroup = new GroupInfo 
    { 
     GroupId = 3, 
     GroupName = "Third group" 
    }; 

    GroupInfo fourthGroup = new GroupInfo 
    { 
     GroupId = 4, 
     GroupName = "Fourth group" 
    }; 

    List<Student> studentList = new List<Student>(); 

    Student firstStudent = new Student(); 
    firstStudent.Name = "Name1"; 
    firstStudent.Age = 15; 
    firstStudent.GroupList = new List<GroupInfo>(); 
    firstStudent.GroupList.Add(firstGroup); 
    firstStudent.GroupList.Add(secondGroup); 
    studentList.Add(firstStudent); 

    Student secondStudent = new Student(); 
    secondStudent.Name = "Name2"; 
    secondStudent.Age = 17; 
    secondStudent.GroupList = new List<GroupInfo>(); 
    secondStudent.GroupList.Add(firstGroup); 
    secondStudent.GroupList.Add(thirdGroup); 
    studentList.Add(secondStudent); 

    Student thirdStudent = new Student(); 
    thirdStudent.Name = "Name3"; 
    thirdStudent.Age = 18; 
    thirdStudent.GroupList = new List<GroupInfo>(); 
    thirdStudent.GroupList.Add(secondGroup); 
    thirdStudent.GroupList.Add(thirdGroup); 
    thirdStudent.GroupList.Add(fourthGroup); 
    studentList.Add(thirdStudent); 

    List<GroupInfo> groupInfoList = new List<GroupInfo>(); 
    // Now What I want is to get a group List Where... 
    foreach (var student in studentList) 
    { 
     // ...First Group Should contains firstStudent and secondStudent 
     // Second group Should firstStudent & thirdStudent 
     // Third group Should contains secondStudent & thirdStuden 
     // Fourth Group Should contains only thirdStudent 
     } 
    } 

一種方法是迭代整個列表並填充GroupInfo列表。只是想知道有沒有其他的方法來完成這項任務。

+0

看看下面的鏈接http://stackoverflow.com/questions/13747344/group-nested-list-with-linq –

+0

東西(x => x.GroupList,(student,groupInfo)=> new {Student = student,Group = groupInfo}) .GroupBy(x => x。)這樣做會做訣竅: var groupedStudents = studentList.SelectMany組); 但是我想你真正想要的是在團體和學生之間添加關係嗎?因此,當您將小組添加到學生的小組列表中時,應該將此學生添加到小組的學生列表中,反之亦然。 –

回答

1

你可以SelectMany這樣做: -

var result = studentList.SelectMany(x => x.GroupList, 
             (studentObj, groups) => new { studentObj, groups }) 
         .GroupBy(x => new { x.groups.GroupId, x.groups.GroupName }) 
         .Select(x => new 
           { 
            GroupId = x.Key.GroupId, 
            GroupName = x.Key.GroupName, 
            Students = x.Select(z => z.studentObj).ToList() 
           }); 

由於您GroupInfo類僅有兩個屬性,即GroupId & GroupName,您將無法獲取與之相關的學生。這就是我從中取出匿名類型的原因。

我得到下面的輸出與此查詢: -

enter image description here

+1

完美,因爲我想拉胡爾。非常感謝。 – Pankaj