LINQ Interview Question #142
How can LINQ perform an anti-join?
Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
An anti-join returns elements from one source that have no matching element in another source. A common expression uses Where with !Any, or a set operation on keys.
Detailed Explanation
An anti-join returns elements from one source that have no matching element in another source. A common expression uses Where with !Any, or a set operation on keys.
For database queries, Where combined with Any is commonly translated to a NOT EXISTS-style query by capable providers.
Code Example
var customersWithoutOrders = customers
.Where(c => !orders.Any(
o => o.CustomerId == c.Id));