LINQ Interview Question #67
What does Union do in LINQ?
Joins, Grouping, Set & Quantifier Operators Mid-Level Intermediate
Quick Interview Answer
Union returns the distinct elements that appear in either of two sequences. Duplicate values are removed according to the equality comparer.
Detailed Explanation
Union returns the distinct elements that appear in either of two sequences. Duplicate values are removed according to the equality comparer.
It differs from Concat because Concat simply appends sequences and preserves duplicates.
Code Example
var first = new[] { 1, 2, 3 };
var second = new[] { 3, 4, 5 };
var result = first.Union(second);
// 1, 2, 3, 4, 5