开发者

Left Outer Join in Entity Data Model asp.net

开发者 https://www.devze.com 2023-03-04 03:26 出处:网络
how to implement Left outer join in Linq to entity framework. DefaultIfEmpty functi开发者_运维技巧on is not supported.

how to implement Left outer join in Linq to entity framework. DefaultIfEmpty functi开发者_运维技巧on is not supported. please provide an example.


This works in .NET 3.5. When you join without doing "from" in combination with the FirstorDefault function, it will give you the row you are looking for in the left joined table. If you want multiple rows, just use where() instead.. Hope this helps.

====

comments = from p in _db.Master

join t in _db.Details on p.DetailID equals t.DetailID into tg

select new 
{

 A = p.Column1,

//this next column is the one from the left joined table

 B = tg.FirstOrDefault(t => t.DetailID == p.DetailID).Column2

};


Entity framework in .NET 3.5 doesn't offer left join in Linq queries. The way to get "joined records" is through navigation property between entities. Something like:

var query = from u in context.Users
            select new 
               {
                   User = u,
                   Orders = u.Orders.Where(...) // Filtered left join but User and Order must be related
               };
0

精彩评论

暂无评论...
验证码 换一张
取 消