【问题标题】:C# Triple Nested Linq GroupC# 三重嵌套 Linq 组
【发布时间】:2011-05-26 00:51:54
【问题描述】:

我有一个问题,我已经简化并一直在摸索。如果我有一个学生列表,并且我想使用 LINQ 按年龄、年级和姓氏生成一组学生,这样我就可以使用嵌套的 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
                }
            }
        }
    }

【问题讨论】:

  • 这是作业吗?如果是这样,请重新标记以表明这一点。
  • 其实不是功课。我的例子要复杂得多,与学生无关,但我对 LINQ 不是很好。

标签: c# linq group-by


【解决方案1】:

怎么样?

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

并遍历 studentGroups 中的每个元素,您可以通过sg.Key.Age 访问Age 等等。

【讨论】:

    【解决方案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)
           {
              //...
           }
         }
       }
    }
    

    【讨论】:

      【解决方案3】:

      为什么不立即将它们分组?

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        相关资源
        最近更新 更多