LINQ Interview Question #64

What are set operators in LINQ?

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

Quick Interview Answer

Set operators combine or compare sequences using set-like semantics. Common operators include Distinct, Union, Intersect, Except, and their key-selector variants available in modern .NET.

Detailed Explanation

Set operators combine or compare sequences using set-like semantics. Common operators include Distinct, Union, Intersect, Except, and their key-selector variants available in modern .NET.

Equality behavior matters because duplicate detection and matching depend on the default or supplied equality comparer.

Code Example

var a = new[] { 1, 2, 3 };
var b = new[] { 3, 4, 5 };

var union = a.Union(b);       // 1,2,3,4,5
var common = a.Intersect(b);  // 3
var except = a.Except(b);     // 1,2