Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Triple Nested Linq Group

Tags:

c#

linq

group-by

I have a problem which I've simplified down and have been scratching my head at. If I have a list of students and I want to use LINQ to produce a group of students by Age, Grade, and LastName so I can traverse them with a nested foreach loop how would I go about this. Here is some code:

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
                }
            }
        }
    }
like image 889
Luke Belbina Avatar asked Feb 24 '23 02:02

Luke Belbina


2 Answers

How about?

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

and looping through each element in studentGroups, you can access Age by sg.Key.Age and so on.

like image 126
Bala R Avatar answered Mar 08 '23 15:03

Bala R


so I can traverse them with a nested foreach loop how would I go about this.

Like this:

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)
       {
          //...
       }
     }
   }
}
like image 32
Amy B Avatar answered Mar 08 '23 14:03

Amy B