首页 > 编程语言 >#yyds干货盘点#【愚公系列】2023年02月 .NET/C#知识点-LINQ和lambda实现左、右、内链接

#yyds干货盘点#【愚公系列】2023年02月 .NET/C#知识点-LINQ和lambda实现左、右、内链接

时间:2023-02-22 23:32:51浏览次数:40  
标签:知识点 name yyds C# List list Add new id

前言

1.左连接

table1居左,故谓之左连接。这种情况下,以table1为主,即table1中的所有记录均会被列出。有一下三种情况:

1、对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且刚好只有一条,那么就会在

返回的结果中形成一条新的记录。如上面Person A和Person B对应的情况。

2、对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person C对应的情况。

3、对于table1中的每一条记录对应的城市如果在table2中不存在,那么就会在返回的结果中形成一条

条新的记录,且该记录的右边全部NULL。如上面的Person D对应的情况。

不符合上面三条规则的记录不会被列出。

在这里插入图片描述

2.右连接

table2居右,故谓之右连接。这种情况下,以table2为主,即table2中的所有记录均会被列出。有一下三种情况:

1、对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且刚好只有一条,那么就会在

返回的结果中形成一条新的记录。如上面Person X和Person Y对应的情况。

2、对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person W对应的情况。

3、对于table2中的每一条记录对应的城市如果在table1中不存在,那么就会在返回的结果中形成一条

条新的记录,且该记录的左边全部NULL。如上面的Person Z对应的情况。

不符合上面三条规则的记录不会被列出。

在这里插入图片描述

3. 内连接

内连接的数据记录中,不会存在字段为NULL的情况。可以简单地认为,内链接的结果就是在左连接或者右连接的结果中剔除存在字段为NULL的记录后所得到的结果。甚至可以认为,如果两个表中仅分别剩下内连接运算后所得的数据记录,如table1中只有Person A、Person B和Person C,table2中只有Person W、Person X和Person Y,那么这两个表的之间的左连接和右连接的返回的结果是一样的。

注意:select * from table1 a inner join table2 b on a.city = b.city 和select * from table1 a join table2 b on a.city = b.city 的效果是一样的,即如果join的左边没有诸如left、right或者inner这样的关键字时,缺省的是内连接。另,MySQL不支持full join。

在这里插入图片描述

一、LINQ和lambda实现左、右、内链接

1.LINQ实现左、右、内链接

1.1 左链接

static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "[email protected]" });
    list.Add(new Customer { id = 2, name = "张学友", email = "[email protected]" });
    list.Add(new Customer { id = 3, name = "黎明", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "[email protected]" });
    return list;
}

static List<CustomerContact> GetCustomerContact()//联系人
{
    List<CustomerContact> list = new List<CustomerContact>();
    list.Add(new CustomerContact { ccid = 1, CustomerId = 1, Phone = "13888888888", Address = "北京" });
    list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13899999999", Address = "天津" });
    list.Add(new CustomerContact { ccid = 3, CustomerId = 2, Phone = "13866666666", Address = "香港" });
    list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13877777777", Address = "上海" });
    return list;
}

//左链接
var LeftJoin = from cusetomer in GetCustomer()
               join cc in GetCustomerContact()
               on cusetomer.id equals cc.CustomerId into JoinCC
               from cc in JoinCC.DefaultIfEmpty()
               select new
               {
                   CustomerName = cusetomer.name,
                   phone = cc != null ? cc.Phone : null
               };

在这里插入图片描述

1.2 右链接

static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "[email protected]" });
    list.Add(new Customer { id = 2, name = "张学友", email = "[email protected]" });
    list.Add(new Customer { id = 3, name = "黎明", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "[email protected]" });
    return list;
}

static List<CustomerContact> GetCustomerContact()//联系人
{
    List<CustomerContact> list = new List<CustomerContact>();
    list.Add(new CustomerContact { ccid = 1, CustomerId = 1, Phone = "13888888888", Address = "北京" });
    list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13899999999", Address = "天津" });
    list.Add(new CustomerContact { ccid = 3, CustomerId = 2, Phone = "13866666666", Address = "香港" });
    list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13877777777", Address = "上海" });
    return list;
}

//右链接
var LightJoin = from cc in GetCustomerContact()
                join cusetomer in GetCustomer()
                on cc.CustomerId equals cusetomer.id into JoinCC
                from cusetomer in JoinCC.DefaultIfEmpty()
                select new
                {
                    phone = cc.Phone,
                    CustomerName = cusetomer != null ? cusetomer.name : null,
                };

在这里插入图片描述

1.3 内链接

static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "[email protected]" });
    list.Add(new Customer { id = 2, name = "张学友", email = "[email protected]" });
    list.Add(new Customer { id = 3, name = "黎明", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "[email protected]" });
    return list;
}

static List<CustomerContact> GetCustomerContact()//联系人
{
    List<CustomerContact> list = new List<CustomerContact>();
    list.Add(new CustomerContact { ccid = 1, CustomerId = 1, Phone = "13888888888", Address = "北京" });
    list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13899999999", Address = "天津" });
    list.Add(new CustomerContact { ccid = 3, CustomerId = 2, Phone = "13866666666", Address = "香港" });
    list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13877777777", Address = "上海" });
    return list;
}

//内链接
var InnerJoin = from cc in GetCustomerContact()
                join cusetomer in GetCustomer()
                on cc.CustomerId equals cusetomer.id
                select new
                {
                    phone = cc.Phone,
                    CustomerName = cusetomer.name
                };

在这里插入图片描述

2.lambda实现左、右、内链接

2.1 左链接和右链接

左链接和有链接一样,只是位置不一样,这边就整合一个案例

static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "[email protected]" });
    list.Add(new Customer { id = 2, name = "张学友", email = "[email protected]" });
    list.Add(new Customer { id = 3, name = "黎明", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "[email protected]" });
    return list;
}

static List<CustomerContact> GetCustomerContact()//联系人
{
    List<CustomerContact> list = new List<CustomerContact>();
    list.Add(new CustomerContact { ccid = 1, CustomerId = 1, Phone = "13888888888", Address = "北京" });
    list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13899999999", Address = "天津" });
    list.Add(new CustomerContact { ccid = 3, CustomerId = 2, Phone = "13866666666", Address = "香港" });
    list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13877777777", Address = "上海" });
    return list;
}

//左链接和右链接
var LMLeftJoin = GetCustomer().GroupJoin(GetCustomerContact(),
 a => a.id, b => b.CustomerId, (a, b) => new { a, b })
     .SelectMany(c => c.b.DefaultIfEmpty(), (c, b)
     => new { CustomerName = c.a.name, Phone = b != null ? b.Phone : null });

在这里插入图片描述

2.2 内链接

static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "[email protected]" });
    list.Add(new Customer { id = 2, name = "张学友", email = "[email protected]" });
    list.Add(new Customer { id = 3, name = "黎明", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "[email protected]" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "[email protected]" });
    return list;
}

static List<CustomerContact> GetCustomerContact()//联系人
{
    List<CustomerContact> list = new List<CustomerContact>();
    list.Add(new CustomerContact { ccid = 1, CustomerId = 1, Phone = "13888888888", Address = "北京" });
    list.Add(new CustomerContact { ccid = 2, CustomerId = 1, Phone = "13899999999", Address = "天津" });
    list.Add(new CustomerContact { ccid = 3, CustomerId = 2, Phone = "13866666666", Address = "香港" });
    list.Add(new CustomerContact { ccid = 4, CustomerId = 8, Phone = "13877777777", Address = "上海" });
    return list;
}

//内链接
var LMInnerJoin = GetCustomer().Join(GetCustomerContact(), a => a.id, b => b.CustomerId
, (a, b) => new { CustomerName = a.name, Phone = b.Phone });

在这里插入图片描述

标签:知识点,name,yyds,C#,List,list,Add,new,id
From: https://blog.51cto.com/u_15437432/6079484

相关文章

  • #yyds干货盘点# LeetCode面试题: 括号生成
    1.简述:数字n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合。 示例1:输入:n=3输出:["((()))","(()())","(())()","()(())","()()()"]......
  • #yyds干货盘点# LeetCode程序员面试金典:交点
    题目:给定两条线段(表示为起点start={X1,Y1}和终点end={X2,Y2}),如果它们有交点,请计算其交点,没有交点则返回空值。要求浮点型误差不超过10^-6。若有多个交点(线段重叠)则返......
  • Centos7 K8S基础镜像设置
    1.1硬件基础配置指标指标值CPU2核内存2048M1.2操作系统基础配置配置项配置值Date&TimeAsia/ShanghaitimezoneKeyBOARDEnglish(US)......
  • WMI Scripting Primer
    WMIScriptingPrimer:Part1Article06/30/200625minutestoread GregStemp,DeanTsaltas,andBobWellsMicrosoftCorporationEthanWilanskyNetwork......
  • 【算法训练营day60】LeetCode84. 柱状图中的最大的矩形
    LeetCode84.柱状图中的最大的矩形题目链接:84.柱状图中的最大的矩形独上高楼,望尽天涯路感觉和接雨水很像。慕然回首,灯火阑珊处思路如下。我们可以遍历每根柱子,以当......
  • 自学python-安装python/pycharm-day03
    安装解释器/学习工具pycharm1.Python简介诞生python的创始人为吉多·范罗苏姆(GuidovanRossum),一般称龟叔。1989年的圣诞节期间,龟叔为了在阿姆斯特丹打发时间,决心......
  • CSS引入方式
                    总结:  ......
  • SONiC系统第三方容器管理
    SONiC系统采用基于容器的架构,包括Redis数据库在内的几个核心部件是运行在操作系统之上的几个容器应用,这个从SONiC的软件架构图上可以明确地看出来。SONiC系统采用基于容器......
  • Spring IOC官方文档学习笔记(十二)之基于Java的容器配置
    1.@Bean与@Configuration(1)标注于类之上的@Configuration注解与标注于方法之上的@Bean注解是支持基于Java的容器配置的核心,被@Bean注解标注的方法用于实例化bean并将其......
  • 修复element ui级联懒加载问题,二次封装成el-cascader-plus
    el-cascader-plus经常碰到懒加载不回显的问题,使用起来很不方便,于是花了些时间二次开发了这个组件,下次遇到同样问题就能直接解决,在此开源出来,希望对遇到相同问题的人有帮......