簡體   English   中英

C# 三重嵌套 Linq 組

[英]C# Triple Nested Linq Group

我有一個問題,我已經簡化了並且一直在摸索。 如果我有一個學生列表,並且我想使用 LINQ 按年齡、年級和姓氏生成一組學生,這樣我就可以使用嵌套的 foreach 循環遍歷他們,我將如何 go 關於這個。 這是一些代碼:

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
                }
            }
        }
    }

怎么樣?

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

並循環遍歷 studentGroups 中的每個元素,您可以通過sg.Key.Age等訪問Age

所以我可以用嵌套的 foreach 循環遍歷它們,我將如何 go 關於這個。

像這樣:

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)
       {
          //...
       }
     }
   }
}

為什么不直接將它們分組?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM