惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 每天进步多一点

linq group by having 实现 常用知识-T-SQL优化 mysql窗口函数、Mysql分析函数 MySQL系列三(定位慢SQL、索引优化、SQL优化)Using filesort MySQL 内存相关参数设置 MySQL COALESCE 函数使用详解 SQL性能优化指南:如何优化MySQL多表join场景 MySQL内部临时表(Using temporary)案例详解及优化解决方法 cookie操作类(加密,获取,删除) MySql 5.7 索引不存在则创建,存在则忽略 SQL SERVER年月周日超止时间 数据抽取的常见理论方法 ETL系列-数据抽取(Extract) 常用时间sql语句 数据库运维:mysql 数据库迁移方法-mysqldump 了解MySQL中的JSON_ARRAYAGG和JSON_OBJECT函数 MySQL的IFNULL()、ISNULL()、NULLIF()函数用法说明 如何看懂explain工具信息,使用explain工具来分析索引 mysql 如何查看sql语句执行时间和效率
C#学习相关系列之Linq用法---group和join相关用法
每天进步多一点 · 2026-03-27 · via 博客园 - 每天进步多一点

一、Group用法

        在C#的LINQ中,Grou将集合中的元素按照指定的键进行分组。Group方法返回一个IEnumerable<IGrouping<TKey, TElement>>类型的集合,其中TKey表示分组的键类型,TElement表示集合中元素的类型。每个IGrouping<TKey, TElement>对象表示一个分组,其中Key属性表示分组的键,而IEnumerable<TElement>表示分组中的元素集合。

1、groupby的定义:

//Linq语句
var tt = from e in stuList group e by e.Math;
//lambda 表达式
var tt = stuList.GroupBy(p=>p.Math);

示例代码:

List<Student_1> stuList = new List<Student_1>()
{
    new Student_1(){ID=1,Name="John",Chinese=92,Math=88,English=92},
    new Student_1(){ID=2,Name="Mary",Chinese=87,Math=94,English=82},
    new Student_1(){ID=3,Name="KangKang",Chinese=89,Math=91,English=96},
    new Student_1(){ID=4,Name="Maria",Chinese=88,Math=65,English=94},
    new Student_1(){ID=5,Name="Ben",Chinese=70,Math=91,English=82},
};
//var tt = from e in stuList group e by e.Math;//linq语句
var tt = stuList.GroupBy(p=>p.Math);//lambda表达式
foreach (var item in tt)
{
    Console.WriteLine(item.Key);
    foreach (var e in item)
   {
        Console.WriteLine("    "+e.Name);
   }
}

运行结果为:

image

 2、group...into...用法可以生成一个temp组

var tt = from e in stuList
         group new { e.Math, e.ID, e.Name } by e.Math  into temp 
         select new { temp.Key, value1 = temp.Select(p => new { p.ID, p.Name }), value2 = temp.Select(p => p.Name) };
//  var tt = stuList.GroupBy(p => p.Math, e => new { e.Math, e.ID, e.Name }).Select(temp => new { temp.Key, value1 = temp.Select(p => new { p.ID, p.Name }), value2 = temp.Select(p => p.Name) });
foreach (var item in tt)
{
    Console.WriteLine(item.Key);
    foreach (var e in item.value1)
    {
        Console.WriteLine(e.Name);
    }
}

image

二、Join用法

Linq中的join用于将两个集合中的元素进行匹配,返回一个新的集合。

1、内部链接 Join  (join… in… on …equals…)

定义:

//使用查询语句 Linq
var list = from T1 in dataSource1
                join T2 in dataSource2 on T1.ID equals T2.ID
                select new {Name = T1.Name,ID=T1.ID,NickName = T2.NickName};
//使用查询方法 lambda 表达式
var list = dataSource1.Join(dataSource2,T1 => T1.ID,T2 => T2.ID, (T1,T2) => new {
        Name = T1.Name,
        ID=T1.ID,
        NickName = T2.NickName
});

代码示例:

List<Student> students = new List<Student>()
{
    new Student() { Id = 1, Name = "Tom" },
    new Student() { Id = 2, Name = "Jerry" },
    new Student() { Id = 3, Name = "Mike" }
};
List<Course> courses = new List<Course>()
{
    new Course() { Id = 1, Name = "Math", StudentId = 1 },
    new Course() { Id = 2, Name = "English", StudentId = 1 },
    new Course() { Id = 3, Name = "Math", StudentId = 2 },
    new Course() { Id = 4, Name = "Science", StudentId = 3 }
};
// 使用join将学生和课程进行匹配
var result = from s in students
                join c in courses on s.Id equals c.StudentId
                select new { StudentName = s.Name, CourseName = c.Name };
// 输出结果
foreach (var item in result)
{
    Console.WriteLine("{0}选了{1}课程", item.StudentName, item.CourseName);
}

image

2、分组链接Groupjoin (join…in…on…equals…into…)

定义:

//lambda 表达式
var result = students.GroupJoin(courses, p => p.Id,s => s.StudentId,(p, e) => new { StudentName = p.Name, CourseName =e.Select(m=>m.Name)});
//linq 语句
var result = from left in students 
join right in courses on left.Id equals right.StudentId into temp
select new { StudentName = left.Name, CourseName = temp.Select(m => m.Name) }; // temp 其实为course的分组

代码示例:

// 创建学生和课程集合
List<Student> students = new List<Student>()
{
    new Student() { Id = 1, Name = "Tom" },
    new Student() { Id = 2, Name = "Jerry" },
    new Student() { Id = 3, Name = "Mike" }
};
List<Course> courses = new List<Course>()
{
    new Course() { Id = 1, Name = "Math", StudentId = 1 },
    new Course() { Id = 2, Name = "English", StudentId = 1 },
    new Course() { Id = 3, Name = "Math", StudentId = 2 },
    new Course() { Id = 4, Name = "Science", StudentId = 3 }
};
// var result = students.GroupJoin(courses, p => p.Id,s => s.StudentId,(p, e) => new { StudentName = p.Name, CourseName =e.Select(m=>m.Name)});
var result = from left in students join right in courses on left.Id equals right.StudentId into temp select new { StudentName = left.Name, CourseName = temp.Select(m => m.Name) };
// temp 其实为course的分组
// 使用join将学生和课程进行匹配
//var result = from s in students
//                join c in courses on s.Id equals c.StudentId
//                select new { StudentName = s.Name, CourseName = c.Name };
// 输出结果
foreach (var item in result)
{
    Console.WriteLine("{0}选了课程", item.StudentName);
        foreach (var n in item.CourseName)
        {
            Console.WriteLine("课程为:{0}",n);
        }
}

运行结果为:

image

通过对比可以发现,join和groupjion的区别在于,join相当于左右一对一关系,例如:

左1=右1,左1=右2....这样一一列举,但groupjoin不一样,他是以左一为关键字,将左一的值生成一个list,例如 左1={右1,右2}...,在实际应用中根据需求选用不同的用法。