GroupJoin Method
Correlates the elements of two sequences based on equality of keys and groups the results.
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector
);
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
IEqualityComparer<TKey> comparer
);
Parameters
Name | Type | Description |
---|---|---|
outer |
IEnumerable<TOuter> |
An IEnumerable<T> whose elements are to be joined. |
inner |
IEnumerable<TInner> |
An IEnumerable<T> whose elements are to be joined to the first sequence. |
outerKeySelector |
Func<TOuter, TKey> |
A function to extract the join key from each element of the outer sequence. |
innerKeySelector |
Func<TInner, TKey> |
A function to extract the join key from each element of the inner sequence. |
resultSelector |
Func<TOuter, IEnumerable<TInner>, TResult> |
A function to create a result element from an element of the outer sequence and a collection of matching elements from the inner sequence. |
comparer |
IEqualityComparer<TKey> |
An IEqualityComparer<TKey> to compare keys. |
Returns
An IEnumerable<TResult>
that contains elements of type TResult
produced by performing a group join operation.
Remarks
The GroupJoin
method performs a left outer join. For each element in the outer sequence, it produces a result element that includes the element from the outer sequence and a sequence of all elements from the inner sequence that have a matching key. If no elements in the inner sequence match the key of an element in the outer sequence, the inner sequence in the result will be empty.
Examples
Example 1: Grouping Customers with Their Orders
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
}
public class Order {
public int OrderId { get; set; }
public int CustomerId { get; set; }
public string Product { get; set; }
}
public static void Main(string[] args) {
List<Customer> customers = new List<Customer> {
new Customer { Id = 1, Name = "Alice" },
new Customer { Id = 2, Name = "Bob" },
new Customer { Id = 3, Name = "Charlie" }
};
List<Order> orders = new List<Order> {
new Order { OrderId = 101, CustomerId = 1, Product = "Laptop" },
new Order { OrderId = 102, CustomerId = 1, Product = "Mouse" },
new Order { OrderId = 103, CustomerId = 2, Product = "Keyboard" }
};
var groupedOrders = customers.GroupJoin(
orders,
customer => customer.Id,
order => order.CustomerId,
(customer, custOrders) => new {
CustomerName = customer.Name,
CustomerOrders = custOrders.Any() ? custOrders : new List<Order>()
}
);
foreach (var item in groupedOrders) {
Console.WriteLine($"Customer: {item.CustomerName}");
if (!item.CustomerOrders.Any()) {
Console.WriteLine(" No orders found.");
} else {
foreach (var order in item.CustomerOrders) {
Console.WriteLine($" - {order.Product} (ID: {order.OrderId})");
}
}
Console.WriteLine();
}
}