首页 > 其他分享 >LINQ 左连接(Left Join)

LINQ 左连接(Left Join)

时间:2022-08-24 16:46:31浏览次数:69  
标签:Join Name AddressId LINQ Address Employee new ID Left

 1. 数据准备

using System.Collections.Generic;

namespace LINQTutorial
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int AddressId { get; set; }
        public static List<Employee> GetAllEmployees()
        {
            return new List<Employee>()
            {
              new Employee { ID = 1, Name = "Preety", AddressId = 1},
                new Employee { ID = 2, Name = "Priyanka", AddressId =2},
                new Employee { ID = 3, Name = "Anurag", AddressId = 0},
                new Employee { ID = 4, Name = "Pranaya", AddressId = 0},
                new Employee { ID = 5, Name = "Hina", AddressId = 5},
                new Employee { ID = 6, Name = "Sambit", AddressId = 6}
            };
        }
    }
    public class Address
    {
        public int ID { get; set; }
        public string AddressLine { get; set; }
        public static List<Address> GetAddress()
        {
            return new List<Address>()
            {
                new Address { ID = 1, AddressLine = "AddressLine1"},
                new Address { ID = 2, AddressLine = "AddressLine2"},
                new Address { ID = 5, AddressLine = "AddressLine5"},
                new Address { ID = 6, AddressLine = "AddressLine6"},
            };
        }
    }
}

2.左连接查询写法

using System;
using System.Linq;

namespace LINQTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            //例子一:
            var query = from emp in Employee.GetAllEmployees()
                        join add in Address.GetAddress() on emp.AddressId equals add.ID into EmpAddGroup
                        from empadd in EmpAddGroup.DefaultIfEmpty()
                        select new { emp, empadd };
            foreach (var item in query)
            {
                Console.WriteLine($"Name:{item.emp.Name}, Address:{item.empadd?.AddressLine}");
            }


            //例子二:
            var query2 = from emp in Employee.GetAllEmployees()
                         join add in Address.GetAddress() on emp.AddressId equals add.ID into EmpAddGroup
                         from empadd in EmpAddGroup.DefaultIfEmpty()
                         select new
                         {
                             EmployeeName = emp.Name,
                             Address = empadd == null ? "NA" : empadd.AddressLine
                         };
            foreach (var item in query2)
            {
                Console.WriteLine($"Name:{item.EmployeeName},Address:{item.Address}");
            }

            Console.ReadLine();
        }
    }
}

3.注意事项

如果有运用到左连接,一定要注意右表可能为空的判断。

个人猜测,其实empadd是代表右表:

 

 

 4.延伸知识

如果要判断一个Employee是否有地址?HasAddress

   //例子三:判断一个Employee是否有地址?
            Console.WriteLine("例子三");
            var query3 = from emp in Employee.GetAllEmployees()
                         join add in Address.GetAddress() on emp.AddressId equals add.ID into EmpAddGroup
                         from empadd in EmpAddGroup.DefaultIfEmpty()
                         select new
                         {
                             EmployeeName = emp.Name,
                             HasAddress = empadd != null
                         };
            foreach (var item in query3)
            {
                Console.WriteLine($"Name:{item.EmployeeName},HasAddress:{item.HasAddress}");
            }

5.测试结果

 

 

 

参考文献
https://dotnettutorials.net/lesson/left-outer-join-in-linq/

自己写的代码在:CSharpBasic\LINQTutorial

标签:Join,Name,AddressId,LINQ,Address,Employee,new,ID,Left
From: https://www.cnblogs.com/keeplearningandsharing/p/16620651.html

相关文章

  • copyleft
    Copyleftisthepracticeofgrantingtherighttofreelydistributeandmodifyintellectualpropertywiththerequirementthatthesamerightsbepreservedin......
  • springboot+mybatis-plus-join+mysql实现连表查询
    1.简介  Mybatis是目前比较主流的持久层框架,使用非常广泛。Mybatis-Plus是基于Mybatis增强工具包,越来越受到开发人员的喜爱。  在使用Mybatis-Plus开发时,简单的crud......
  • Lambda方式左连接有Linq方式左连接
    http://t.zoukankan.com/superfeeling-p-7530549.htmlhttps://blog.csdn.net/qq_22325259/article/details/121545038 网上查到的直接使用Join+DefaultIfEmpty的方式是......
  • 记C# 通过JObject 读取 json对象(Newtonsoft.Json.Linq.JObject.this[string].get 返回
    json对象"RequestHeaders":{ "Host":"tool.kkbmtj.com", "Referer":"https://m.kkbmtj.com/ys/shortindex?origin=kktj&xcx", }代码:HeaderLogheaderLog......
  • 结构体数组使用StructLinq
    .NET性能优化-为结构体数组使用StructLinq 前言本系列的主要目的是告诉大家在遇到性能问题时,有哪些方案可以去优化;并不是要求大家一开始就使用这些方案来提升性能。......
  • What does Include() do in LINQ?
    WhatdoesInclude()doinLINQ?问题ItriedtodoalotofresearchbutI'mmoreofadbguy-soeventheexplanationintheMSDNdoesn'tmakeanysensetom......
  • sql语句左链接left join--3张表关联
    表A---------------------------------关联第一张表B-----------------------关联第二张表cselect*from表名Aleftjoin 表B  on  表A字段=表B的idlef......
  • CAD二次开发---关于JoinEntity出现eNotApplicable的问题
    作者在使用JoinEntity时出现eNotApplicable的问题,查阅了Autodesk论坛的相关帖子,发现大多数人都有遇到这个问题,但没有找到合适的解决方法,可能原因是进行Join时两Curve需要同......
  • Linq-20220817更新
    一、常用函数Where:每一项数据都会经过predicate(传入的委托lambda表达式)的测试,如果对元素执行predicate后返回值为True,则这个元素会添加到结果数组中Count:每一项数据都......
  • Linq
    一、常用函数Where:每一项数据都会经过predicate(传入的委托lambda表达式)的测试,如果对元素执行predicate后返回值为True,则这个元素会添加到结果数组中Count:每一项数据都......