C # Triple Nested Linq Group

I have a problem that I have simplified and scratched his head. If I have a list of students, and I want to use LINQ to create a group of students by age, class, and the LastName, so I can cross them with a foreach nested loop, as if I did. Here is the 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 } } } } 
+4
source share
3 answers

What about?

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

and scrolling through each element in student groups, you can get access to Age on sg.Key.Age and so on.

+5
source

so I can cross them with a foreach nested loop, as if I did.

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) { //... } } } } , student.LastName} into g2 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
source

Why not group them at once?

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

All Articles