Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: SQL LEFT JOIN clause varies based on declarative- vs. method-style join #33745

Open
smarts opened this issue May 17, 2024 · 0 comments

Comments

@smarts
Copy link

smarts commented May 17, 2024

Redirected here from npgsql/efcore.pg#3176

  1. Declarative-style equijoin
from foo in dbContext.Set<Foo>()
join bar in dbContext.Set<Bar>() on
new { foo.Id, State = "Blah" } equals new { Id = bar.FooId, bar.State } into barGroupJoin
from subBar in barGroupJoin.DefaultIfEmpty()
select new { /* use subBar instead of bar */ }
  1. Method-style equijoin
from foo in dbContext.Set<Foo>()
from bar in dbContext.Set<Bar>().Where(x => x.FooId == foo.Id && x.State == "Blah").DefaultIfEmpty()
select new { /* use bar */ }

I generally prefer 2 because it's arguably just as easy to read, doesn't leave a variable that goes unused, etc.
But one thing I notice is that the former style will generate something like

LEFT JOIN mySchema."Bars" AS B ON B."FooId" = F."Id" AND B."State" = 'Blah'

while the latter generates something like

LEFT JOIN (
SELECTFROM mySchema."Bars" AS B
WHERE B."State" = 'Blah'
) as C ON C."FooId" = F."Id"

Is this intentional, and if so why?

Include provider and version information

EF Core version: 8.0.4
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 8
Operating system: MacOS Sonoma 14.4.1
IDE: Rider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant