C#Triple Nested Linq Group

时间:2011-05-26 00:51:55

标签: c# linq group-by

我有一个问题,我已经简化了并且一直在摸不着头脑。如果我有一个学生列表,并且我想使用LINQ按Age,Grade和LastName生成一组学生,那么我可以使用嵌套的foreach循环遍历它们,我将如何解决这个问题。这是一些代码:

class Student
    {
        public int Grade;
        public int Age;
        public string LastName;
    }

    public void SomeMethod()
    {
        Student student1 = new Student() { Age = 10, Grade = 100, LastName = "Smith"};
        Student student2 = new Student() { Age = 10, Grade = 90, LastName = "Jones" };
        Student student3 = new Student() { Age = 10, Grade = 50, LastName = "Bob" };
        Student student4 = new Student() { Age = 10, Grade = 100, LastName = "Smith" };
        Student student5 = new Student() { Age = 11, Grade = 10, LastName = "Bob" };
        Student student6 = new Student() { Age = 11, Grade = 30, LastName = "Jones" };
        Student student7 = new Student() { Age = 13, Grade = 90, LastName = "Bob" };
        Student student8 = new Student() { Age = 13, Grade = 90, LastName = "Smithy" };
        Student student9 = new Student() { Age = 15, Grade = 100, LastName = "Smith" };
        Student student10 = new Student() { Age = 15, Grade = 0, LastName = "Smith" };

        List<Student> studentList = new List<Student>()
                                     {
                                         student1,student2,student3,student4,student5,student6,student7,student8,student9,student10
                                     };

        var studentGroups = from student in studentList
                            group student by student.Age into studentAgeGroup
                            from studentAgeGradeGroup in
                                              (
                                                  from student in studentAgeGroup
                                                  group student by student.Grade
                                              )
                            group studentAgeGradeGroup by studentAgeGroup.Key;

        foreach (var ageGroup in studentGroups)
        {                
            foreach (var gradeGroup in ageGroup)
            {
                foreach (var innerGroupElement in gradeGroup)
                {
                     // At this point they are grouped by age and grade but I would also like them to be grouped by LastName
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:5)

怎么样?

var studentGroups = from student in studentList
            group student by new {student.Age, student.Grade, student.LastName};

并循环遍历studentGroups中的每个元素,您可以Age访问sg.Key.Age,依此类推。

答案 1 :(得分:2)

  

所以我可以使用嵌套的foreach循环遍历它们,我将如何解决这个问题。

像这样:

var studentGroups =
  from student in studentList
  group student by new {student.Age, student.Grade, student.LastName} into g2
  group g2 by new {g2.Key.Age, g2.Key.Grade} into g1
  group g1 by g1.Key.Age


foreach(var group0 in studentGroups)
{
   int age = group0.Key;
   foreach(var group1 in group0)
   {
     int grade = group1.Key.Grade
     foreach(var group2 in group1)
     {
       string lastName = group2.Key.LastName
       foreach(Student s in group2)
       {
          //...
       }
     }
   }
}

答案 2 :(得分:1)

为什么不马上把它们分组?

var studentGroups = from student in studentList 
                    group student by new { student.Age, student.Grade, student.LastName } 
                    into studentAgeGroup 
                    select studentAgeGroup;