LINQ:GroupByMultipleKeys

一、数据准备

public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Branch { get; set; }
        public int Age { get; set; }
        public static List<Student> GetAllStudents()
        {
            return new List<Student>()
        {
            new Student { ID = 1001, Name = "玲玲", Gender = "Female",
                                         Branch = "CSE", Age = 20 },
            new Student { ID = 1002, Name = "张三", Gender = "Male",
                                         Branch = "ETC", Age = 21  },
            new Student { ID = 1003, Name = "李四", Gender = "Male",
                                         Branch = "CSE", Age = 21  },
            new Student { ID = 1004, Name = "王五", Gender = "Male",
                                         Branch = "CSE", Age = 20  },
            new Student { ID = 1005, Name = "珊珊", Gender = "Female",
                                         Branch = "ETC", Age = 20 },
            new Student { ID = 1006, Name = "涵涵", Gender = "Female",
                                         Branch = "CSE", Age = 21 },
            new Student { ID = 1007, Name = "钱六", Gender = "Male",
                                         Branch = "CSE", Age = 22  },
            new Student { ID = 1008, Name = "婷婷", Gender = "Female",
                                         Branch = "CSE", Age = 20  },
            new Student { ID = 1009, Name = "兰兰", Gender = "Female",
                                         Branch = "ETC", Age = 22 },
            new Student { ID = 1010, Name = "黄九", Gender = "Male",
                                         Branch = "ETC", Age = 21 }
        };
        }
    }

二、GroupByMultipleKeys

 //Using Query Syntax
            Console.WriteLine("方式一:");
            var GroupByMultipleKeysQS = from std in Student.GetAllStudents()
                                        group std by new
                                        {
                                            std.Branch,
                                            std.Gender
                                        } into stdGroup
                                        orderby stdGroup.Key.Branch descending,
                                        stdGroup.Key.Gender ascending
                                        select new
                                        {
                                            Branch = stdGroup.Key.Branch,
                                            Gender = stdGroup.Key.Gender,
                                            Students = stdGroup.OrderBy(x => x.Name)
                                        };
            foreach (var group in GroupByMultipleKeysQS)
            {
                Console.WriteLine($"Branch:{group.Branch} Gender:{group.Gender} No of Students={group.Students.Count()}");
                foreach (var student in group.Students)
                {
                    Console.WriteLine($"ID:{student.ID},Name:{student.Name},Age:{student.Age}");
                }
            }
            //Using Query Syntax
            Console.WriteLine("方式二:");
            var GroupByMultipleKeysMS = Student.GetAllStudents()
                .GroupBy(x => new { x.Branch, x.Gender })
                .OrderByDescending(g => g.Key.Branch).ThenBy(g => g.Key.Gender)
                .Select(g => new
                {
                    Branch = g.Key.Branch,
                    Gender = g.Key.Gender,
                    Students = g.OrderBy(x => x.Name)
                });
            foreach (var group in GroupByMultipleKeysMS)
            {
                Console.WriteLine($"Branch:{group.Branch} Gender:{group.Gender} No of Students={group.Students.Count()}");
                foreach (var student in group.Students)
                {
                    Console.WriteLine($"ID:{student.ID},Name:{student.Name},Age:{student.Age}");
                }
            }

三、测试结果

 

 四、参考网址

https://dotnettutorials.net/lesson/groupby-multiple-keys-in-linq/

个人代码见....CSharpBasic\LINQTutorial..
posted @ 2022-08-26 17:13  katesharing  阅读(37)  评论(0编辑  收藏  举报