Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Tip/Trick

Left Outer Join extension for Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.86/5 (7 votes)
11 Feb 2014CPOL 101.1K   610   16   5
Snippet for Left Outer Join, Entity Framework

Introduction 

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:

C#
var leftJoin = p.Person
                       .GroupJoin(p.PersonInfo, 
                                  n => n.PersonId,
                                  m => m.PersonId,
                                  (n, ms) => new { n, ms = ms.DefaultIfEmpty() })
                       .SelectMany(z => z.ms.Select(m => 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: 

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

 Background 

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

C#
// 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: 

C#
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: 

SQL
 SELECT 
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 

History

Initial post.

License

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


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt report a bug Pin
lichao88722-Jun-14 1:32
lichao88722-Jun-14 1:32 
AnswerRe: It report a bug Pin
Tony Sherlock2-Jun-14 2:04
Tony Sherlock2-Jun-14 2:04 
QuestionNice idea, but download is not working Pin
eiiv10-Feb-14 22:28
eiiv10-Feb-14 22:28 
AnswerRe: Nice idea, but download is not working Pin
Tony Sherlock11-Feb-14 1:05
Tony Sherlock11-Feb-14 1:05 
GeneralThank you, it works now Pin
eiiv11-Feb-14 12:38
eiiv11-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.