首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【愚公系列】2023年01月 .NET/C#知识点-LINQ和lambda实现左、右、内链接

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

作者头像
愚公搬代码
发布2023-03-16 15:55:44
发布2023-03-16 15:55:44
79400
代码可运行
举报
文章被收录于专栏:历史专栏历史专栏
运行总次数:0
代码可运行

文章目录


前言

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 左链接

代码语言:javascript
代码运行次数:0
运行
复制
static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "ldh@net.cn" });
    list.Add(new Customer { id = 2, name = "张学友", email = "zxy@net.cn" });
    list.Add(new Customer { id = 3, name = "黎明", email = "lm@net.cn" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "gfc@net.cn" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "gtl@net.cn" });
    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 右链接

代码语言:javascript
代码运行次数:0
运行
复制
static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "ldh@net.cn" });
    list.Add(new Customer { id = 2, name = "张学友", email = "zxy@net.cn" });
    list.Add(new Customer { id = 3, name = "黎明", email = "lm@net.cn" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "gfc@net.cn" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "gtl@net.cn" });
    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 内链接

代码语言:javascript
代码运行次数:0
运行
复制
static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "ldh@net.cn" });
    list.Add(new Customer { id = 2, name = "张学友", email = "zxy@net.cn" });
    list.Add(new Customer { id = 3, name = "黎明", email = "lm@net.cn" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "gfc@net.cn" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "gtl@net.cn" });
    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.LINQ实现左、右、内链接

2.1 左链接和右链接

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

代码语言:javascript
代码运行次数:0
运行
复制
static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "ldh@net.cn" });
    list.Add(new Customer { id = 2, name = "张学友", email = "zxy@net.cn" });
    list.Add(new Customer { id = 3, name = "黎明", email = "lm@net.cn" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "gfc@net.cn" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "gtl@net.cn" });
    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 内链接

代码语言:javascript
代码运行次数:0
运行
复制
static List<Customer> GetCustomer()//客户
{
    List<Customer> list = new List<Customer>();
    list.Add(new Customer { id = 1, name = "刘德华", email = "ldh@net.cn" });
    list.Add(new Customer { id = 2, name = "张学友", email = "zxy@net.cn" });
    list.Add(new Customer { id = 3, name = "黎明", email = "lm@net.cn" });
    list.Add(new Customer { id = 4, name = "郭富城", email = "gfc@net.cn" });
    list.Add(new Customer { id = 4, name = "古天乐", email = "gtl@net.cn" });
    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 });
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023/01/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
    • 1.左连接
    • 2.右连接
    • 3. 内连接
  • 一、LINQ和lambda实现左、右、内链接
    • 1.LINQ实现左、右、内链接
      • 1.1 左链接
      • 1.2 右链接
      • 1.3 内链接
    • 2.LINQ实现左、右、内链接
      • 2.1 左链接和右链接
      • 2.2 内链接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档