C# Linq Jion用法
没用过数据库,对这个扩展方法不是很理解,今天记录一下 大概意思就是在A,B两个序列中,根据条件查找相同的项
Msdn官方介绍 Join 操作(C#) | Microsoft Docs
-
Join 和 GroupJoin是连接运算符。
-
Join 类似于SQL的内部连接。它返回一个新集合,其中包含两个键匹配的集合中的公共元素。
-
Join 对内部序列和外部序列这两个序列进行运算,并生成结果序列。
数据库中有左连接和右连接,linq就一个Jion(反过来操作即可实现右连接) 先看简单例子
再看复杂一点的
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApp5 8 { 9 internal class Program 10 { 11 private static void Main(string[] args) 12 { 13 var list1 = new List<Student>(); 14 list1.Add(new Student() { Id = 1, Name = "韩梅梅" }); 15 list1.Add(new Student() { Id = 2, Name = "李磊" }); 16 list1.Add(new Student() { Id = 3, Name = "张三", Age = 15 }); 17 list1.Add(new Student() { Id = 4, Name = "刘能" }); 18 list1.Add(new Student() { Id = 4, Name = "李四" }); 19 var list2 = new List<Student>(); 20 list2.Add(new Student() { Id = 2, Name = "韩梅梅" }); 21 list2.Add(new Student() { Id = 1, Name = "李磊" }); 22 list2.Add(new Student() { Id = 3, Name = "张三", Age = 18 }); 23 list2.Add(new Student() { Id = 7, Name = "王五" }); 24 list2.Add(new Student() { Id = 8, Name = "李四" }); 25 list2.Add(new Student() { Id = 4, Name = "赵六" }); 26 //查询list1中与list2中 Name 相同的项 27 var query1 = list1.Join(list2, s => s.Name, s => s.Name, (s1, s2) => s1); 28 foreach (var stu in query1) 29 { 30 Console.WriteLine($"{stu.Id}---{stu.Name}"); 31 } 32 33 Console.WriteLine("==================="); 34 //查询list2中Id与list1相同的项, 35 //可以注意到,结果的顺序是按照list1中查询到的顺序排列 36 var query2 = list1.Join(list2, s => s.Id, s => s.Id, (s1, s2) => s2); 37 foreach (var stu in query2) 38 { 39 Console.WriteLine($"{stu.Id}---{stu.Name}"); 40 } 41 42 Console.WriteLine("==================="); 43 var query3 = list1.Join(list2, s => new { s.Id, s.Name }, s => new { s.Id, s.Name }, (s1, s2) => s1); 44 //查询list1中Id,Name在list2中都相同的项 45 foreach (var stu in query3) 46 { 47 Console.WriteLine($"{stu.Id}---{stu.Name}---{stu.Age}"); 48 } 49 } 50 } 51 52 internal class Student 53 { 54 public int Age { get; set; } 55 public int Id { get; set; } 56 public string Name { get; set; } 57 } 58 }
不同类型的查询
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApp5 8 { 9 internal class Program 10 { 11 private static void Main(string[] args) 12 { 13 var list1 = new List<Student>(); 14 list1.Add(new Student() { Id = 1, Name = "韩梅梅" }); 15 list1.Add(new Student() { Id = 2, Name = "李磊" }); 16 list1.Add(new Student() { Id = 3, Name = "张三", Age = 15 }); 17 list1.Add(new Student() { Id = 4, Name = "刘能" }); 18 list1.Add(new Student() { Id = 4, Name = "李四" }); 19 var list2 = new List<Standard>(); 20 list2.Add(new Standard() { Id = 1, Name = "一年级" }); 21 list2.Add(new Standard() { Id = 2, Name = "二年级" }); 22 list2.Add(new Standard() { Id = 3, Name = "三练级" }); 23 //查询list1中与list2中id相同的项,并输出到一个匿名类 24 var query1 = list1.Join( 25 list2, 26 stu => stu.Id, 27 sta => sta.Id, 28 (s1, s2) => new { s1.Id, s1.Name, Standard = s2.Name }); 29 foreach (var item in query1) 30 { 31 Console.WriteLine($"{item.Id}---{item.Name}----{item.Standard}"); 32 } 33 } 34 } 35 36 internal class Standard 37 { 38 public int Id { get; set; } 39 public string Name { get; set; } 40 } 41 42 internal class Student 43 { 44 public int Age { get; set; } 45 public int Id { get; set; } 46 public string Name { get; set; } 47 } 48 }
GroupJion
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApp5 8 { 9 internal class Program 10 { 11 private static void Main(string[] args) 12 { 13 var list1 = new List<Student>(); 14 list1.Add(new Student() { Id = 1, Name = "韩梅梅" }); 15 list1.Add(new Student() { Id = 2, Name = "李磊" }); 16 list1.Add(new Student() { Id = 2, Name = "李雷" }); 17 list1.Add(new Student() { Id = 2, Name = "李蕾" }); 18 list1.Add(new Student() { Id = 3, Name = "张三", Age = 15 }); 19 list1.Add(new Student() { Id = 3, Name = "张散", Age = 15 }); 20 list1.Add(new Student() { Id = 3, Name = "张叁", Age = 15 }); 21 list1.Add(new Student() { Id = 4, Name = "刘能" }); 22 list1.Add(new Student() { Id = 4, Name = "李四" }); 23 var list2 = new List<Standard>(); 24 list2.Add(new Standard() { Id = 2, Name = "二年级" }); 25 list2.Add(new Standard() { Id = 3, Name = "三练级" }); 26 list2.Add(new Standard() { Id = 5, Name = "三练级" }); 27 //按年级分类学生列表 28 var query1 = list2.GroupJoin( 29 list1, 30 stu => stu.Id, 31 sta => sta.Id, 32 (s1, s2) => new { Standard = s1, Students = s2 }); 33 foreach (var item in query1) 34 { 35 Console.WriteLine($"{item.Standard.Name}"); 36 foreach (var stu in item.Students) 37 { 38 Console.WriteLine($"{stu.Name}"); 39 } 40 Console.WriteLine(""); 41 } 42 } 43 } 44 45 internal class Standard 46 { 47 public int Id { get; set; } 48 public string Name { get; set; } 49 } 50 51 internal class Student 52 { 53 public int Age { get; set; } 54 public int Id { get; set; } 55 public string Name { get; set; } 56 } 57 }
另外还有一个方法重载,传入IEqualityComparer比较器 用法和这个类似
分类:
C#基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)