SQL to LINQ-左联接两个条件

时间:2018-07-13 00:21:51

标签: c# linq

如何将外部左联接转换为LINQ?

    SELECT *  from table1 t1
      left join table2 t2 on  t1.code = t2.code AND  t2.id ='xxxxx'
       WHERE t1.someId = 2

我正在尝试这样的事情:

from t1 in db.table1
join t2 in db.table2 on t1.Code equals t2.Code
into oj
from sub in oj.DefaultIfEmpty()
where t1.someId == 2
where (sub.id == 'xxxx')

但不是返回左表中的所有行。我认为where子句在连接后应用。

1 个答案:

答案 0 :(得分:1)

var res=(from t1 in table1.Where(x=>x.someId==2)
         join t2 in table2.Where(y=>y.id==xxxx)
         on t1.code = t2.code
         into r 
         from t3 in r.DefaultIfEmpty()
         select new {t1,t3}).ToList();