LINQ Interview Question #53

How do you perform an inner join using method syntax?

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

Quick Interview Answer

Use the Join extension method. It receives the inner sequence, an outer key selector, an inner key selector, and a result selector.

Detailed Explanation

Use the Join extension method. It receives the inner sequence, an outer key selector, an inner key selector, and a result selector.

The result selector determines the shape produced for every matching pair.

Code Example

var result = customers.Join(
    orders,
    c => c.Id,
    o => o.CustomerId,
    (c, o) => new CustomerOrderDto
    {
        CustomerName = c.Name,
        OrderId = o.Id
    });