/*
* 经常看到有人问linq语法如何写left join的查询语句,但网上找到的都是简单的两表连接,参考意义有限。
* 今天终于项目里要用到复杂的多表连接,同时含有多个左连接,
* 恰好又遇到业务中有复杂连接条件,因此整理如下,供大家参考:
*/
list = from t1 in Content.table1
join t2 in Content.table2
on t1.colum1 equals t2.colum2
into r1
from t2 in r1.DefaultIfEmpty()
join t3 in Content.table3
on new { pid = t2.colum2, sid = t2.colum1, t2.colum3 } //字段类型或值不一致时,
equals new { pid = t3.colum2 ?? 0, sid = t3.colum3 + 1, t3.colum4 } //可以这样统一起来
into r2
from t3 in r2.DefaultIfEmpty()
join t4 in Content.table4
on t2.colum5 equals t4.colum1
where t2.id == paramId
&& string.IsNullOrEmpty(t1.colum2)
select new your-result-class-name { //这里写点啥,地球人都知道。。。 }
EntityFramework经典的left join语法
最新推荐文章于 2024-07-13 16:28:12 发布