1

how to implement Left outer join in Linq to entity framework. DefaultIfEmpty function is not supported. please provide an example.

2
  • 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. Commented May 7, 2011 at 9:10
  • @Jared Harding: I am using .net 3.5, what to do to implement left outer join in entitydatamodel. Commented May 7, 2011 at 9:18

2 Answers 2

4

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

};
Sign up to request clarification or add additional context in comments.

Comments

3

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
               };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.