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

  • Home
  • SEARCH
  • 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 315873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:19:54+00:00 2026-05-12T08:19:54+00:00

I need to do a left join on multiple conditions where the conditions are

  • 0

I need to do a left join on multiple conditions where the conditions are ORs rather than ANDs. I’ve found lots of samples of the latter but am struggling to get the right answer for my scenario.

from a in tablea
join b in tableb on new { a.col1, a.col2 } equals new { b.col1, b.col2 }
group a by a into g
select new () { col1 = a.col1, col2 = a.col2, count = g.Count() }

works great for joins where all conditions must match. I need to get the join to match on a.col1 = b.col1 OR a.col2 = b.col2.

I know it must be easy but I’ve coming up blank on this!

Edit:

To give a little more info, the purpose of the query is to get a projection containing all of the fields from ‘a’ plus a count of the matching records in ‘b’. I’ve amended the sample above to try and illustrate what I’m after. When I run with the above using the approach Jon Skeet has noted I’m getting a count of all records from a, not the count of the related records in b.

The basic left join works fine:

from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty()
select new { col1 = a.col1, col2 = a.col2 }

If I revise it to add the grouping as below

from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty()
group a by a.col1 into g
select new { col1 = g.Key, count = g.Count() }

I’m getting the count of the records returned from a – not the count of records in matching in b.

Edit:

I’ll give the answer to Jon – I’ve solved my count issue – I hadn’t realized I could use a lamda to filter the count (g.Count(x => x != null)). Plus I need to group b by a rather than a by a as I had above. This gives the correct result but the SQL is not as efficient as I’d write it by hand as it adds a correlated sub query – if anyone can advise a better way of writing it to simulate the following SQL I’d appreciate it!

select a.col1, count(b.col1)
from tablea a
left join tableb b
on a.col1 = b.col1
or a.col2 = b.col2
group by a.col1
  • 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-12T08:19:54+00:00Added an answer on May 12, 2026 at 8:19 am

    LINQ only directly supports equijoins. If you want to do any other kind of join, you basically need a cross-join and where:

    from a in tablea
    from b in tableb
    where a.col1 == b.col1 || a.col2 == b.col2
    select ...
    

    It’s probably worth checking what the generated SQL looks like and what the query plan is like. There may be more efficient ways of doing it, but this is probably the simplest approach.

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

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 244k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code as published doesn't look like it should give… May 13, 2026 at 8:05 am
  • Editorial Team
    Editorial Team added an answer Try this $root = "http://localhost/"; $sql = mysql_query("SELECT * FROM… May 13, 2026 at 8:05 am
  • Editorial Team
    Editorial Team added an answer Actually, you're not showing the configuration of your transaction manager… May 13, 2026 at 8:05 am

Related Questions

So I've asked a couple of questions about performing joins and have had great
Does anyone know of a way to do a left outer join with SubSonic
I asked a question earlier about why left joins in Linq can't use defined
I have two tables (notes and tags). Tags has a foreign key to notes.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.