Click here to Skip to main content
15,399,688 members
Articles / Entity Framework
Posted 12 Jun 2014

Tagged as


6 bookmarked

Simplest Way to Make an OUTER JOIN in LINQ to Entity

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Jun 2014CPOL
The title says it all! Make an outer join in 1 LINQ statement


Here, I will show how to write an OUTER JOIN in LINQ to SQL in the simplest fashion (one line)!

Using the Code

Let's say you got the following class in your Entity Framework model:

public class Product
    public int ID { get; set; }
    public string Name { get; set; }
class Comment
    public int ID { get; set; }
    public int ProductID { get; set; }

    public string Text { get; set; }

And let's say you would like to get a product and the first comment or null, i.e., make an outer join...

Fear not, it's easy indeed!!

from p in ctx.Products
let c = (from c in ctx.Comments where c.ProductID == p.ID select c).FirstOrDefault()
select new { p, c }

This will make an outer join statement!

Points of Interest

I keep discovering everyday that LINQ to SQL / Entity is better than I thought!


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


About the Author

Super Lloyd
Software Developer (Senior)
Australia Australia
The Australia born French man who went back to Australia later in life...
Finally got over life long (and mostly hopeless usually, yay!) chronic sicknesses.
Worked in Sydney, Brisbane, Darwin, Billinudgel, Darwin and Melbourne.

Comments and Discussions

SuggestionTyping mistake Pin
GeorgiV16-Jun-14 20:53
professionalGeorgiV16-Jun-14 20:53 
GeneralRe: Typing mistake Pin
Super Lloyd16-Jun-14 23:34
MemberSuper Lloyd16-Jun-14 23:34 

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.