CKTIS / C#
Enumerable.GroupJoin: practical reference
Enumerable.GroupJoin: practical reference: For each outer element, creates a pair with the sequence of all inner elements having an equal key, including an empty sequence.
Version: .NET LINQ; verified on .NET 10.0.14
Syntax
source.GroupJoin(/* overload-specific arguments */)For each outer element, creates a pair with the sequence of all inner elements having an equal key, including an empty sequence.
Use it for grouped joins and, after adding DefaultIfEmpty, left joins.
The inner source is indexed in memory. Key and result selectors can throw during enumeration.
No match produces an empty group rather than removing the outer element. Null sources or selectors cause ArgumentNullException.
The complete program actually uses Enumerable.GroupJoin; its exact expected stdout is A:2,B:0.
Examples
using System;
using System.Collections.Generic;
using System.Linq;
var left = new[] { (Id: 1, Name: "A"), (Id: 2, Name: "B") };
var right = new[] { (Owner: 1, Value: 10), (Owner: 1, Value: 20) };
Console.WriteLine(string.Join(",", left.GroupJoin(right, item => item.Id, item => item.Owner, (item, matches) => $"{item.Name}:{matches.Count()}")));
Result
A:2,B:0
The complete program actually uses `Enumerable.GroupJoin`; its exact expected stdout is `A:2,B:0`.