How to use left join in linq which we use in sql?

How can I use Left join in Linq to write an SQL query?

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 
+4
source share
4 answers

Use the Join keyword instead of Left join, and be sure to use the INTO keyword and the DefaultIfEmpty () method, since the right table returns null.

  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(); 
0
source

Do it like this:

 var query = from p in personnel join e in Employee on p.EmployTypecode equals e.EmployTypecode into temp from j in temp.DefaultIfEmpty() select new { name = p.name, family = p.family, EmployTypecode = String.IsNullOrEmpty(j.EmployTypecode) ? "" : j.EmployTypecode, ...... } 
0
source
 var q=( from pd in dataContext.personnel join od in dataContext.Employee on pd.EmployTypecode equals od.EmployTypecode into t from rt in t.DefaultIfEmpty() orderby pd.EmployTypecode select new { EmployTypecode=(int?)rt.EmployTypecode, pd.Name, pd.Family, rt.EmplytyppeTye } ).ToList(); 
0
source

Why don't you use SQL query to convert EF to LIST. In EF 6.1

records

  public class personnel { public String Name { get; set; } public String Family { get; set; } public String EmployTypecode { get; set; } public String employtypeName { get; set; } public String EmplytyppeTye { get; set; } } List<personnel> personnels = dbentities.Database.SqlQuery<personnel>(@"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 ").ToList(); 
0
source

All Articles