skip to content

In EF Core 10, how do you write a SQL LEFT JOIN in LINQ, and how did you have to express it before .NET 10?

level: middleimportance: should knowfreq 34%

answer

  1. new .NET 10 Queryable operator
  2. LeftJoin(inner, outerKey, innerKey, result)
  3. inner side may be null in the selector
  4. old shape: GroupJoin, SelectMany, DefaultIfEmpty
  5. no query-syntax keyword yet

basics

~20 s

Call the .NET 10 LeftJoin operator in method syntax, outer.LeftJoin(inner, outerKey, innerKey, (o, i) => ...), and EF Core 10 emits LEFT JOIN. Before .NET 10 you chained GroupJoin, SelectMany and DefaultIfEmpty in one exact shape.

solid answer

~40 s

In .NET 10, `Queryable.LeftJoin(inner, outerKeySelector, innerKeySelector, resultSelector)` is a real LINQ operator, and EF Core 10 translates it to `LEFT JOIN`; in the result selector the inner element is `null` when nothing matches, so the projection needs a null check. `RightJoin` exists too and becomes `RIGHT JOIN`. C# query syntax has no keyword for either yet, so it is method syntax only. Before .NET 10 the left join was a pattern: `GroupJoin`, then `SelectMany` over the group with `DefaultIfEmpty()`, and EF only recognised it when the flattening came straight after the `GroupJoin`. When a navigation property exists, I often skip explicit joins: selecting an optional navigation already makes EF emit a `LEFT JOIN`.

go deeper

for a junior

Know that .NET 10 has a LeftJoin method and that EF Core 10 turns it into a SQL LEFT JOIN.

for a middle

Write LeftJoin with its key selectors, handle the null inner element, and recognise the older GroupJoin, SelectMany and DefaultIfEmpty pattern in existing code.

for a senior

Prefer navigations when the model relates the sets, use explicit joins only for unrelated data, and verify the emitted join type with ToQueryString.

for a principal

Decide whether a codebase migrates old left-join patterns during an upgrade or leaves them, weighing churn against readability.

## The new operator .NET 10 added `LeftJoin` and `RightJoin` LINQ operators, including `Queryable.LeftJoin` and `Queryable.RightJoin` for `IQueryable` sources. EF Core 10 recognises both and translates them to `LEFT JOIN` and `RIGHT JOIN`. On the project dashboard, listing work items with an optional assignee taken from a members table that has no navigation property: ```csharp var rows = await db.WorkItems .Where(w => w.ProjectId == projectId) .LeftJoin( db.Members, w => w.AssigneeId, m => m.Id, (w, m) => new { w.Title, Assignee = m == null ? "unassigned" : m.DisplayName }) .ToListAsync(); ``` EF Core 10 sends one statement whose `FROM` clause reads `"WorkItems" AS w LEFT JOIN "Members" AS m ON w."AssigneeId" = m."Id"`, with the project filter in the `WHERE` clause and the null check turned into a SQL `CASE`. Points to remember: - The signature mirrors `Join`: inner source, outer key selector, inner key selector, result selector. - In the result selector the **inner element can be `null`**; handle it explicitly. - **Query syntax** (`from w in ... join ...`) has no left-join keyword yet, so these operators are **method syntax only**. - `RightJoin` keeps every row of the inner source instead. ## How it was done before Before .NET 10, LINQ had no left join operator. The documented pattern was: 1. `GroupJoin` the two sources on the key, giving each outer element a group of matches. 2. `SelectMany` over that group with `DefaultIfEmpty()`, so an empty group yields one `null`. 3. Project from the outer element and the possibly-null inner one. ```csharp var rows = await db.WorkItems .GroupJoin(db.Members, w => w.AssigneeId, m => m.Id, (w, ms) => new { w, ms }) .SelectMany(x => x.ms.DefaultIfEmpty(), (x, m) => new { x.w.Title, Assignee = m!.DisplayName }) .ToListAsync(); ``` EF Core recognised this only when the flattening **immediately followed** the `GroupJoin`; other arrangements of the same operators might not be identified as a left join and could fail to translate. That fragility, plus the readability cost, is why the new operator matters. ## When you need no join at all If the model has a navigation, for example `WorkItem.Assignee` configured as optional, you rarely write a join: | Query | SQL | |---|---| | `Select(w => new { w.Title, w.Assignee!.DisplayName })` over an optional navigation | `LEFT JOIN` generated by EF | | Same over a required navigation | `INNER JOIN` | | `LeftJoin(...)` against an unrelated set | `LEFT JOIN` on the keys you give | Explicit joins are for sets the model does not relate: a lookup table without a foreign key, a keyless view, or an ad hoc correlation. ## RightJoin and null handling `RightJoin` is the mirror image: every row of the **inner** source survives, and the outer element is `null` when nothing matches. EF Core 10 translates it to `RIGHT JOIN`. Most teams reorder the sources and use `LeftJoin` instead, because a left join reads more naturally, but both are supported. Null handling in the result selector deserves attention in either direction: 1. With nullable reference types enabled, the compiler flags member access on the possibly-null side; write an explicit `m == null ? ... : ...` or use the null-coalescing operator on the member. 2. In SQL, a missing match yields `NULL` columns, and EF turns the C# null check into a `CASE` or `COALESCE` expression, so the check costs nothing on the client. 3. A value-type column from the inner side, such as an `int` story-point count, must be projected as `int?` or given a default, otherwise materializing a `NULL` fails. ## Interview angle - Naming the operator and knowing that it is **new in .NET 10 / EF Core 10** shows current knowledge. - Explaining the old `GroupJoin` / `SelectMany` / `DefaultIfEmpty` pattern shows you can read existing code bases, most of which still contain it. - Mentioning that navigations produce joins for you shows you would not hand-write joins the model already describes.

  • Can you write the new left join with C# query syntax in .NET 10?
    No. C# query expressions have no keyword for `LeftJoin` or `RightJoin` yet, so you call the methods directly. In query syntax you still write `join ... into g` followed by `from x in g.DefaultIfEmpty()`, which compiles to the old `GroupJoin` / `SelectMany` pattern that EF also translates.
  • Why can the old GroupJoin pattern fail to translate when rearranged?
    EF identifies a left join by recognising `GroupJoin` followed immediately by `SelectMany` over `DefaultIfEmpty()`. If other operators sit between them, or the grouping is used another way, the expression no longer matches that pattern and EF may not be able to translate it, throwing a could-not-be-translated error.

  • EF Core cannot express a LEFT JOIN without raw SQL.
  • Join with DefaultIfEmpty() on the outer side gives a left join.
  • The new LeftJoin operator works in C# query syntax.
  • LeftJoin never yields null for the inner element.

Assumes EF Core 10 on .NET 10, where Queryable.LeftJoin and RightJoin exist and EF translates them; earlier versions need the GroupJoin, SelectMany and DefaultIfEmpty pattern.