日期:2014-05-20  浏览次数:20651 次

Linq Left Join条件
如下SQL的Linq该咋个写?
select A.* from A left join B on (A.c1=B.c1 and B.c2='N')
where A.c3>9

------解决方案--------------------
var query= from a in A.Where(x=>x.c3>9)
           join b in B.Where(y=>y.c2=="N")
           on a.c1 equals b.c1 into lg
           from b in lg.DefaultIfEmpty()
           select a;

------解决方案--------------------
关键是 XX.DefaultIfEmpty()

并且要进行异常处理如果XX中的元素是NULL的情况,需要:

from r in o.DefaultIfEmpty()
select new { 
ID=r.ID, 
Name=(r==null)?"":r.Name
}