C# Linq Jion用法

没用过数据库,对这个扩展方法不是很理解,今天记录一下 大概意思就是在A,B两个序列中,根据条件查找相同的项

Msdn官方介绍 Join 操作(C#) | Microsoft Docs

  1. Join 和 GroupJoin是连接运算符。

  2. Join 类似于SQL的内部连接。它返回一个新集合,其中包含两个键匹配的集合中的公共元素。

  3. 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比较器 用法和这个类似

posted @ 2022-04-26 11:04  只吃肉不喝酒  阅读(863)  评论(0编辑  收藏  举报