LINQ Interview Question #70

What does Except do in LINQ?

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

Quick Interview Answer

Except returns distinct elements from the first sequence that do not appear in the second sequence. It is a set-difference operation.

Detailed Explanation

Except returns distinct elements from the first sequence that do not appear in the second sequence. It is a set-difference operation.

The direction matters: first.Except(second) is not generally the same as second.Except(first).

Code Example

var existing = new[] { 1, 2, 3, 4 };
var removed = new[] { 2, 4 };

var remaining = existing.Except(removed);

// 1, 3