LINQ Interview Question #54

What is GroupJoin in LINQ?

Joins, Grouping, Set & Quantifier Operators Mid-Level Intermediate

Quick Interview Answer

GroupJoin correlates each element from the outer sequence with a collection of matching elements from the inner sequence. It is conceptually similar to a grouped relational join.

Detailed Explanation

GroupJoin correlates each element from the outer sequence with a collection of matching elements from the inner sequence. It is conceptually similar to a grouped relational join.

It is useful for parent-and-children projections such as each customer together with all of that customer's orders.

Code Example

var result = customers.GroupJoin(
    orders,
    c => c.Id,
    o => o.CustomerId,
    (customer, customerOrders) => new
    {
        customer.Name,
        Orders = customerOrders
    });