Proposal: introduce left and right join clauses to C# query expression syntax #8892
Unanswered
roji
asked this question in
Language Ideas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Introduce
left join
andright join
clauses into LINQ query expression syntax, e.g.:Motivation
LINQ has a Join operator, which, like its SQL INNER JOIN counterpart, correlates elements of two sequences based on matching keys; the C# language has a corresponding
join
clause which translates to this operator (section 12.20.3.5 of the C# specs).In addition to INNER JOIN, SQL also has LEFT JOIN, which returns outer elements even if there's no corresponding inner ones; LINQ and C#, in contrast, lack this operator. The LINQ conceptual documentation shows how to combine existing operators to achieve a left join:
Or using method syntax:
Although functionality sufficient for expressing a left join operation, this combining approach has the following drawbacks:
dotnet/runtime#110292 is an approved API proposal for introducing LeftJoin and RightJoin operators into the System.Linq for .NET 10; see that proposal for additional performance information and benchmarks. Aside from allowing users to more easily express SQL LEFT/RIGHT JOIN when using a LINQ provider (such as EF Core), it would also allow both easier and more efficient in-memory left/right joins, exactly in the way that inner joins are already supported.
Along with the introduction of the operators into System.Linq, the C# query expression syntax can be extended with new
left join
andright join
clauses which would translate to these new operators, allowing for simpler and more efficient code where C# query syntax is used.Design
The syntax for the proposed
left join
andright join
expression clauses would be identical to the existingjoin
clause. For example:Note that after the
left join
clause,department
is nullable, asleft join
also returns students with no corresponding departments (unlike the existingjoin
clause, which does not).Similar to how the
join
clause internally translates to the LINQJoin
operator,left join
would translate to the newLeftJoin
operator being added in dotnet/runtime#110292:right join
represents the opposite operation, returning inner items when no corresponding outer ones are found:This would translate to:
Notes
left join
/right join
, or possiblyleftjoin
/rightjoin
(similar to theorderby
clause)./cc @eiriktsarpalis @terrajobst @CyrusNajmabadi @dotnet/efteam
Previous related issues
Beta Was this translation helpful? Give feedback.
All reactions