如何在sql中使用的linq中使用Left join?

ehs*_*adi 5 c# sql linq

如何在编写SQL查询的Linq中使用左联接?

select 
    p.Name, p.Family,
    E.EmployTypecode, E.employtypeName, E.EmplytyppeTye 
from 
    personnel as p
left join 
    Employee as E on E.EmployTypecode = p.EmployTypecode 
Run Code Online (Sandbox Code Playgroud)

Muh*_*ker 3

使用Join关键字而不是Left join,并且必须使用“INTO”关键字和“DefaultIfEmpty()”方法,因为右表返回空值。

   var query = from p in personnel 
               join e in Employee on p.EmployTypecode equals e.EmployTypecode into t
               from nt in t.DefaultIfEmpty()
               orderby p.Name

    select new
    {
        p.Name, p.Family,
        EmployTypecode=(int?)nt.EmployTypecode,  // To handle null value if Employtypecode is specified as not null in Employee table.
        nt.employtypeName, nt.EmplytyppeTye
    }.ToList();
Run Code Online (Sandbox Code Playgroud)