[EF] - 全连接

在EntityFramework里有个DeflautIfEmpty方法可以用来表示数据库里的左联接或者右连接:

http://msdn.microsoft.com/en-us/library/bb397895.aspx

但是MS没有给出全连接对应的方法,这个比较坑。

研究了下,发现其实全链接就相当于左联接+右连接,所以在LINQ查询里可以对要连接的表做两次查询,然后用Union或者Concat连接起来就是一个全连接了:

var leftoutrtjoin = (from order in db.Orders

                                         join team in db.Teams on order.OrderID equals team.TeamID into ot

                                         from otnew in ot.DefaultIfEmpty()

                                         select new { OrderID = order.OrderID, TeamID = otnew == null ? 0 : otnew.TeamID }).ToList();


                    var rightouterjoin = (from team in db.Teams

                                          join order in db.Orders on team.TeamID equals order.OrderID into ot

                                          from otnew in ot.DefaultIfEmpty()

                                          select new { OrderID = otnew == null ? 0 : otnew.OrderID, TeamID = team.TeamID }).ToList();


                    var fullouterjoin = leftoutrtjoin.Concat(rightouterjoin);

 

posted @ 2014-10-13 17:58  码农小包  阅读(900)  评论(0编辑  收藏  举报