我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘诀就是在join后面加上一个函数DefaultIfEmpty函数,实际上这个函数在linq中貌似也只有将inner join转换为left join的作用,示例如下

var joinResult = DB.Table1s.Join(DB.Table2s, a => a.id, b => b.id, (a, b) => new {a,b} ).DefaultIfEmpty(); 

这样返回的joinResult就包含DB.Table1s left join DB.Table2s的结果了。如果要实现right join将DB.Table1s和DB.Table2s的位置返过来即可,但是目前似乎在linq lambda表达式中没有很好的办法实现full outer join,如果真的用到了full outer join还是老老实实在数据库写视图或者存储过程等吧。。。

我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘诀就是在join后面加上一个函数DefaultIfEmpty函数,实际上这个函数在linq中貌似也只有将inner join转换为left join的作用,示例如下

var joinResult = DB.Table1s.Join(DB.Table2s, a => a.id, b => b.id, (a, b) => new {a,b} ).DefaultIfEmpty(); 

这样返回的joinResult就包含DB.Table1s left join DB.Table2s的结果了。如果要实现right join将DB.Table1s和DB.Table2s的位置返过来即可,但是目前似乎在linq lambda表达式中没有很好的办法实现full outer join,如果真的用到了full outer join还是老老实实在数据库写视图或者存储过程等吧。。。

相关文章:

  • 2022-12-23
  • 2021-12-12
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2021-05-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-05-28
  • 2022-12-23
相关资源
相似解决方案