LINQ Interview Question #143
How can LINQ perform a semi-join?
Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
A semi-join returns elements from the first source when at least one matching element exists in the second source, without returning the matching second-source rows.
Detailed Explanation
A semi-join returns elements from the first source when at least one matching element exists in the second source, without returning the matching second-source rows.
Where combined with Any expresses this intent clearly and avoids duplicate outer elements that a regular inner join might produce.
Code Example
var customersWithOrders = customers
.Where(c => orders.Any(
o => o.CustomerId == c.Id));