Proposal: introduce left and right join clauses to C# query expression syntax #8892
Replies: 6 comments 25 replies
This comment has been hidden.
This comment has been hidden.
-
Please also include a FullJoin! |
Beta Was this translation helpful? Give feedback.
-
@houbi56 The API for left/right outer join was already approved during the .NET API review on 2024-12-03, but full join was omitted, because no demand was seen for this operation. |
Beta Was this translation helpful? Give feedback.
-
Kinda surprised this made it through review so easily. My opinion has been that new methods are just as problematic as new expression nodes, in that existing queryable providers must be updated to support both or they will explode at runtime. Obviously that's true of any and all methods, but such fundamental operations as That's not to say that I'm opposed to the addition of these new methods, quite the contrary. I hope that their addition might help to open up the wider conversation about adding new expression nodes so that expression trees can support newer language features as well. |
Beta Was this translation helpful? Give feedback.
-
I will champion this. |
Beta Was this translation helpful? Give feedback.
-
@roji I've watched the API review and I've heard that you don't exclude the possibility of adding anti joins at a later date. Anti joins are a surprisingly useful concept that I use a lot when working with data frames. I know it's just a |
Beta Was this translation helpful? Give feedback.
-
Summary
left join
andright join
query expression clauses #8947Introduce
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