Click here to Skip to main content
13,094,983 members (76,312 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 10 Feb 2014

Left Outer Join extension for Entity Framework

, 11 Feb 2014
Rate this:
Please Sign up or sign in to vote.
Snippet for Left Outer Join, Entity Framework


Entity Framework doesn't allow to do left outer join in generic way easily. Every time is is needed developers need to remember quite complex snippet like this:

var leftJoin = p.Person
                                  n => n.PersonId,
                                  m => m.PersonId,
                                  (n, ms) => new { n, ms = ms.DefaultIfEmpty() })
                       .SelectMany(z => => new { n = z.n, m )); 

 It is quite difficult to remember when it is used rarely.

This snippet allows to do it much easier like this: 

var joined = dbContext.TableA.LeftOuterJoin(dbContext.TableB, a => a.Id, b => b.IdA, 
         (a, b) => new { a, b, hello = "Hello World!" }); 


Outer join with a LINQ could be implemented in 3 steps:  

// group join as usual + use DefaultIfEmpty
var q1 = Queryable.GroupJoin(db.TableA, db.TableB, a => a.Id, b => b.IdA, 
                              (a, b) => new { a, groupB = b.DefaultIfEmpty() });
// regroup data to associated list a -> b, it is usable already, but it's 
// impossible to use resultSelector on this stage, 
// beacuse of type difference (quite deep problem: some anonymous type != TOuter)
var q2 = Queryable.SelectMany(q1, x => x.groupB, (a, b) => new { a.a, b });
// second regroup to get the right types
var q3 = Queryable.SelectMany(db.TableA, 
                               a => q2.Where(x => x.a == a).Select(x => x.b), 
                               (a, b) => new {a, b}); 

For implementing this steps as an extesnsion we need to build expression trees manually (and it is the hardest part which could be made much easy by this extension).   

Using the code

All the code is quite huge and can be found in article attachment. 

This code could be used easily as shown in introduction part: 

var joined = dbContext.TableA.LeftOuterJoin(dbContext.TableB, a => a.Id, b => b.IdA, 
         (a, b) => new { a, b, hello = "Hello World!" }); 

And the generated SQL could look like: 

1 AS [C1], 
[Extent1].[Id] AS [Id], 
[Extent1].[Text] AS [Text], 
[Join1].[Id1] AS [Id1], 
[Join1].[IdA] AS [IdA], 
[Join1].[Text2] AS [Text2], 
N'Hello World!' AS [C2]
FROM  [A] AS [Extent1]
INNER JOIN  (SELECT [Extent2].[Id] AS [Id2], [Extent2].[Text] AS [Text], [Extent3].[Id]    AS [Id1], [Extent3].[IdA] AS [IdA], [Extent3].[Text2] AS [Text2]
    FROM  [A] AS [Extent2]
    LEFT OUTER JOIN [B] AS [Extent3] ON [Extent2].[Id] = [Extent3].[IdA] ) AS [Join1] ON [Extent1].[Id] = [Join1].[Id2] 

And it is quite good. 

Points of Interest 

There were many hard parts in creating such extensions:

  • Creating complex trees manually (compiler will not help us here) 
  • Reflection is needed for such methods like WhereSelect, etc
  • Avoided using of anonymous types and replace them with Tuples 


Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tony Sherlock
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt report a bug Pin
lichao88722-Jun-14 1:32
memberlichao88722-Jun-14 1:32 
AnswerRe: It report a bug Pin
Tony Sherlock2-Jun-14 2:04
memberTony Sherlock2-Jun-14 2:04 
QuestionNice idea, but download is not working Pin
eiiv10-Feb-14 22:28
membereiiv10-Feb-14 22:28 
AnswerRe: Nice idea, but download is not working Pin
Tony Sherlock11-Feb-14 1:05
memberTony Sherlock11-Feb-14 1:05 
GeneralThank you, it works now Pin
eiiv11-Feb-14 12:38
membereiiv11-Feb-14 12:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 11 Feb 2014
Article Copyright 2014 by Tony Sherlock
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid