有关Entity framework left join

http://www.codeproject.com/Tips/74914/LINQ-To-Entity-Outer-Join

http://blog.csdn.net/foren_whb/article/details/40151079

http://blog.csdn.net/kingcruel/article/details/15341507

http://www.cnblogs.com/OpenCoder/p/4214099.html


var query = (from s in ctx.Students.Include("ClassRooms")
                         join sd in ctx.StudentDescriptions on s.StudentID equals sd.StudentID into g
                         from stuDesc in g.DefaultIfEmpty()
                         select new
                                    {
                                        Name=s.StudentName,
                                        StudentId=s.StudentID,

                         }).SingleOrDefault();


在Linq to sql 和 Entity framework 中使用lambda表达式实现left 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还是老老实实在数据库写视图或者存储过程等吧。。。

20150925更新:红色部分说描述方法,应该是坑爹的说法,经测不对

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值