how to implement Left outer join in Linq to entity framework. DefaultIfEmpty function is not supported. please provide an example.
-
The DefaultIfEmpty function is supported in EntityFramework 4. Perhaps you could give us some more information about your scenario, what data you're trying to use, and which version of EF you're using.Jared Harding– Jared Harding2011-05-07 09:10:24 +00:00Commented May 7, 2011 at 9:10
-
@Jared Harding: I am using .net 3.5, what to do to implement left outer join in entitydatamodel.Mohan– Mohan2011-05-07 09:18:22 +00:00Commented May 7, 2011 at 9:18
Add a comment
|
2 Answers
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
};
Comments
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
};