首页 > 其他分享 >LINQ:GroupByMultipleKeys

LINQ:GroupByMultipleKeys

时间:2022-08-26 17:23:30浏览次数:46  
标签:GroupByMultipleKeys Name Gender Age LINQ Branch new ID

一、数据准备

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

标签:GroupByMultipleKeys,Name,Gender,Age,LINQ,Branch,new,ID
From: https://www.cnblogs.com/keeplearningandsharing/p/16628265.html

相关文章

  • LINQ和委托随意转化例子参考
    staticvoidMain(string[]args){//LINQ简化Action<string>f1=s=>Console.WriteLine(s);//委托Action<string>f11=delegate(strings){Console.Writ......
  • LINQ:Group Join
    一、数据准备publicclassEmployee{publicintID{get;set;}publicstringName{get;set;}publicintAddressId{get;set;......
  • LINQ: Inner Join
    一、数据准备publicclassEmployee{publicintID{get;set;}publicstringName{get;set;}publicintAddressId{get;s......
  • LINQ:Group Join
    1.代码//groupjoinvargroupJoinQS=fromaddrinAddress.GetAddress()joinempinEmployee.GetAllEmployees()onad......
  • Linq-面试题
    1.用逗号分隔表示成绩的字符串,计算成绩的平均值//10,60,30,20,70,90stringstr="10,60,30,20,70,90";string[]strs=str.Split(',');IEnumerable<int>nums=strs......
  • LINQ 左连接(Left Join)
     1.数据准备usingSystem.Collections.Generic;namespaceLINQTutorial{publicclassEmployee{publicintID{get;set;}publics......
  • Lambda方式左连接有Linq方式左连接
    http://t.zoukankan.com/superfeeling-p-7530549.htmlhttps://blog.csdn.net/qq_22325259/article/details/121545038 网上查到的直接使用Join+DefaultIfEmpty的方式是......
  • 记C# 通过JObject 读取 json对象(Newtonsoft.Json.Linq.JObject.this[string].get 返回
    json对象"RequestHeaders":{ "Host":"tool.kkbmtj.com", "Referer":"https://m.kkbmtj.com/ys/shortindex?origin=kktj&xcx", }代码:HeaderLogheaderLog......
  • 结构体数组使用StructLinq
    .NET性能优化-为结构体数组使用StructLinq 前言本系列的主要目的是告诉大家在遇到性能问题时,有哪些方案可以去优化;并不是要求大家一开始就使用这些方案来提升性能。......
  • What does Include() do in LINQ?
    WhatdoesInclude()doinLINQ?问题ItriedtodoalotofresearchbutI'mmoreofadbguy-soeventheexplanationintheMSDNdoesn'tmakeanysensetom......