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


前言

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.LINQ实现左、右、内链接

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 });

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/aa2528877987/article/details/128750812