Repository navigation
Closed
Closed
Copy link
Labels
Milestone
Description
Motivation and Background
GroupJoin currently forces users to pass in a result selector which determines the shape of the return type. In most basic usages, users project out to an anonymous type (or a tuple) which wraps the returned TInner and TOuters - this proposal adds an additional overload of GroupJoin that removes to need to specify a result selector, and returns a tuple. We expect this will cover and simplify a large majority of the use-cases.
This proposal mirrors the simplified Zip overload, and also #120596 for Join.
API Proposal
namespace System.Linq;
public static partial class Enumerable
{
public static IEnumerable<IGrouping<TOuter, TInner>> GroupJoin<TOuter, TInner, TKey>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
IEqualityComparer<TKey>? comparer = null);
}
public static partial class Queryable
{
public static IQueryable<IGrouping<TOuter, TInner>> GroupJoin<TOuter, TInner, TKey>(
this IQueryable<TOuter> outer,
IEnumerable<TInner> inner,
Expression<Func<TOuter, TKey>> outerKeySelector,
Expression<Func<TInner, TKey>> innerKeySelector,
IEqualityComparer<TKey>? comparer = null);
}
public static class AsyncEnumerable
{
public static IAsyncEnumerable<IGrouping<TOuter, TInner>> GroupJoin<TOuter, TInner, TKey>(
this IAsyncEnumerable<TOuter> outer,
IAsyncEnumerable<TInner> inner,
Func<TOuter, CancellationToken, ValueTask<TKey>> outerKeySelector,
Func<TInner, CancellationToken, ValueTask<TKey>> innerKeySelector,
IEqualityComparer<TKey>? comparer = null);
public static IAsyncEnumerable<IGrouping<TOuter, TInner>> Join<TOuter, TInner, TKey>(
this IAsyncEnumerable<TOuter> outer,
IAsyncEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
IEqualityComparer<TKey>? comparer = null);
}Usage
var departments = new List<Department>
{
new() { Id = 1, Name = "HR" },
new() { Id = 2, Name = "IT" },
new() { Id = 3, Name = "Finance" }
};
var employees = new List<Employee>
{
new() { Name = "Alice", DepartmentId = 1 },
new() { Name = "Bob", DepartmentId = 2 },
new() { Name = "Charlie", DepartmentId = 2 },
new() { Name = "David", DepartmentId = 3 }
};
// New proposed simplified usage:
foreach (var (emp, depts) in employees.GroupJoin(departments, e => e.DepartmentId, d => d.Id))
{
Console.WriteLine($"Employee: {emp.Name}, departments: {string.Join(", ", depts.Select(i => i.Name))}");
}
// Existing usage: explicit projection out to tuple required
foreach (var (emp, depts) in employees.GroupJoin(departments, e => e.DepartmentId, d => d.Id, (e, d) => (e, d)))
{
Console.WriteLine($"Employee: {emp.Name}, departments: {string.Join(", ", depts.Select(i => i.Name))}");
}
class Department
{
public int Id { get; set; }
public string Name { get; set; }
}
class Employee
{
public string Name { get; set; }
public int DepartmentId { get; set; }
}Reacted by Copilot
Activity
dotnet-policy-service commentedon Oct 10, 2025
Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.
KalleOlaviNiemitalo commentedon Oct 10, 2025
TResult cannot be deduced from those arguments. Where do the TResult values come from?
Thaina commentedon Oct 10, 2025
Sorry let me fix it
KalleOlaviNiemitalo commentedon Oct 10, 2025
If I understand your proposal correctly, the Enumerable side would be equivalent to the following, but more efficient:
The Queryable side would presumably just build an expression tree and call
source.Provider.CreateQuery<TResult>, like the other methods there. System.Linq.EnumerableRewriter might not need any changes.Thaina commentedon Oct 10, 2025
Correct, in actuality I just want the API to be flatten tuple like
Zipfor convenient to be used in linq andforeacheiriktsarpalis commentedon Oct 20, 2025
Why wouldn't this return
(TOuter, IEnumerable<TInner>)following the spirit of your other proposal in #120596?Thaina commentedon Oct 20, 2025
@eiriktsarpalis 1 - Because
IGroupingis already established interface for group of object with Key. It would be more familiar for people2 - Because it has already has another function with similar signature. For
Joinit was more similar toZipwhileGroupJoinis more similar toGroupBy3 -
IGroupingcan iterate by itselfroji commentedon Oct 30, 2025
I think it's problematic for the returned IGrouping to have
(TKey, TOuter)as its key:Because of the above, I think it would make more sense (and be more consistent) for GroupJoin to return TOuter and the enumerable of correlated TInners. @eiriktsarpalis you suggested this above - but with a tuple as opposed to an IGrouping. I do agree with @Thaina that IGrouping has the advantage of being directly enumerable without having to dot in. But one slight problem with IGrouping is that its generic type parameter is named TKey, and it would be (again) problematic to have that contain the TOuter of the GroupJoin... The sample above obscures this slightly since the outer key selector simply returns TOuter itself as the key (
key => key).I admit that this really is a relatively minor naming concern: if we really want to, we could introduce another IGrouping-like interface to address this (it would be identical to IGrouping except for having the type parameter called TOuter instead of TKey). Or we could be OK with IGrouping's TKey referring to the GroupJoin's TOuter, or just return a tuple as @eiriktsarpalis suggested (dotting into the tuple in order to enumerate isn't the end of the world).
roji commentedon Nov 2, 2025
Discussed offline with @eiriktsarpalis, and amended the above proposal - tl;dr don't return the correlation key, and return a tuple instead of IGrouping (though I think the latter can be discussed).
7 remaining items
bartonjs commentedon Nov 18, 2025
Video
manandre commentedon Nov 25, 2025
Updated proposal with equality comparer support and overloads for
IAsyncEnumerable:API Proposal (v2)
@roji @eiriktsarpalis Could you please make this updated version approved?
eiriktsarpalis commentedon Nov 26, 2025
Done.
[-][API Proposal]: Add a GroupJoin overload returning a tuple of joined entries.[/-][+]Add a GroupJoin overload without a result selector, returning an IGrouping[/+]Add GroupJoin overload returning IGrouping (#121999)
Add GroupJoin overload returning IGrouping (dotnet#121999)
Add GroupJoin overload returning IGrouping (dotnet#121999)