Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6166209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:17:55+00:00 2026-05-23T22:17:55+00:00

There are at least two ways of doing LEFT OUTER JOIN in LINQ class

  • 0

There are at least two ways of doing LEFT OUTER JOIN in LINQ

class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

    static void Main()
    {
        // Example customers.
        var customers = new Customer[]
        {
            new Customer{ID = 5, Name = "Sam"},
            new Customer{ID = 6, Name = "Dave"},
            new Customer{ID = 7, Name = "Julia"},
            new Customer{ID = 8, Name = "Sue"},
            new Customer{ID = 9, Name = "Joe"}
        };

        // Example orders.
        var orders = new Order[]
        {
            new Order{ID = 5, Product = "Book"},
            new Order{ID = 6, Product = "Game"},
            new Order{ID = 7, Product = "Computer"},
            new Order{ID = 8, Product = "Shirt"},
            new Order{ID = 8, Product = "TShirt"}
        };


        // First Way
        var query = from c in customers
                join o in orders on c.ID equals o.ID into co
                from x in co.DefaultIfEmpty() 
                where x != null && x.Product == "Shirt"
                select new {c, x};

        // Second Way  
        var query2 = from c in customers
                     from o in orders
                     where c.ID == o.ID && o.Product == "Shirt"
                     select new {c, o};
    }

I found a lot of examples of First Way (using ‘into’), and that is the way I used to do my LEFT OUTER JOINS. Recently I found the second way, and looks like it is simpler. I tested and both of them produce the same result. Now my question are there any hidden differences, performance, or it is only syntactic sugar?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:17:57+00:00Added an answer on May 23, 2026 at 10:17 pm

    The First way should be used if you don’t use the !=null check

    A left outer join selects everything from the left table with all the items that match in the right table, if no matches exist in the right table the result is still returned but null for the values in the right table (linq translates this as the default value for the collection item type)

    Both your queries effectively perform an inner join so they will be the same.

    To get different results

     // First Way
     var query = from c in customers
     join o in orders on c.ID equals o.ID into co
     from x in co.DefaultIfEmpty()
     where c.Id>5 
     select new {c, x};
    
     // Second Way
     var query2 = from c in customers
     from o in orders
     where c.ID == o.ID && c.ID > 5
     select new {c, o};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

POSIX environments provide at least two ways of accessing files. There's the standard system
From previous post, I learnt that for there are two ways, at least, to
Well, there are at least two low-level ways of determining whether a given number
There are at least two ways that I know of to write a Symbian
There appears to be at least two ways to add a default constraint using
I'm interested in opinions on design. There are at least two ways to locate
There's at least two Bcrypt implementations in C out there: The original: http://bcrypt.sourceforge.net/ Openwall's:
I am trying to print 1 if there are at least two of the
I am trying to get at least three words separated by two commas.I have
Is there a way to get a list ordered by two fields, say last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.