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 8607089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:15:36+00:00 2026-06-12T03:15:36+00:00

I have written a Linq query to do an outer join on multiple columns.

  • 0

I have written a Linq query to do an outer join on multiple columns. My problem is that I want to OR the columns. In other words, I need the join condition:

on Bar.Id equals Foo.BarId1 or Bar.Id equals Foo.BarId2

Some examples I have seen on this site have used anonymous types to create AND conditions, but I can’t figure out how to construct an OR using anonymous type or if it is even possible. I can find no example that matches.

My query:

    var data = (from f in Foos
                join b in Bars on f.BarId1 equals b.Id into tb
                  from xb in tb.DefaultIfEmpty()
                join b in Bars on f.BarId2 equals b.Id into tb2
                  from xb2 in tb2.DefaultIfEmpty()
                select new { Foo = f, Bar1 = xb, Bar2 = xb2 });

This works, but I have a feeling it isn’t the best solution

EDIT

I spoke too soon. It doesn’t work: when Foo.BarId1 points to a valid Bar, and Foo.BarId2 does not, it actually returns the same Bar for Bar1 and Bar2. I was expecting Bar1 to be valid and Bar2 to be null in this case.

EDIT

I’m getting closer. This is my latest query:

var data = from f in Foos
            from b in Bars.Where(x => x.Id == p.BarId1 || x.Id == p.BarId2).DefaultIfEmpty()
            select new { Foo = p, Bars = b };

I was expecting it to return:

Foo Bar    
f1  b1      
    b2

What I get is:

Foo Bar
f1  b1
f1  b2

EDIT

I’ve finally found the query that returns exactly what I want:

var data = from f in Foos
       select new
       {
           Foo = f,
           Bar1 = Bars.FirstOrDefault(x => x.Id == f.Bar1Id),
           Bar2 = Bars.FirstOrDefault(x => x.Id == f.Bar2Id)
       };

Would still like to know of any improvements I can make to this.

FINAL EDIT

I have gone back to the original query:

    var data = (from f in Foos
                join b in Bars on f.BarId1 equals b.Id into tb
                  from xb in tb.DefaultIfEmpty()
                join b in Bars on f.BarId2 equals b.Id into tb2
                  from xb2 in tb2.DefaultIfEmpty()
                select new { Foo = f, Bar1 = xb, Bar2 = xb2 });

It turns out that this was in fact working, but an error in my Linqpad test made me think it wasn’t.

It is more efficient than the previous one too – in SQL Profiler I can see that it generates 1 SQL select, compared to the previous, which generates 3*n selects (does 2 Bar selects for every Foo select)

  • 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-06-12T03:15:37+00:00Added an answer on June 12, 2026 at 3:15 am

    It can be very simple if you are willing to introduce navigation properties Foo.Bar1 and Foo.Bar2. Then you can just do:

    from f in Foos
    select new { f, f.Bar1, f.Bar2 }
    

    This will create outer joins in SQL. (Which may be a reason not to like these navigation properties, because it is easy to forget that they cause outer joins, without them you’re always urged to take control yourself).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a LINQ to XML query that does what I want, but
I currently have a LINQ query written in one my controllers that I want
I have this linq query that works well (although it may be written better,
I have written a LINQ query with or condition in it, but its not
I have a LINQ query that uses 1 table + a large number of
The linq query below is not returning results that have been ordered by the
I have recently written a LINQ query to get a Dictionary containing the last
I understand that if you have some function in a linq query that does
I have a linq query written to read in a .csv file: var csv
I have a LINQ query written to pull at least one row from a

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.