Roadmap › LINQ — Language Integrated Query › Module: Grouping, Joins and Aggregation
Lessons in this course (12)

8. Set operators and generation

IntermediateMediumLesson 8 of 12
csharp
int[] a = [1, 2, 3, 4], b = [3, 4, 5];
a.Union(b);        // 1 2 3 4 5  (distinct)
a.Concat(b);       // 1 2 3 4 3 4 5  (keeps duplicates)
a.Intersect(b);    // 3 4
a.Except(b);       // 1 2
a.Distinct();
products.DistinctBy(p => p.Category);                 // .NET 6
customersA.ExceptBy(customersB.Select(c => c.Id), c => c.Id);

Enumerable.Range(1, 10);              // 1..10
Enumerable.Repeat("x", 3);            // x x x
Enumerable.Empty<int>();
a.Zip(b, (x, y) => x * y);            // pairs element-wise
a.Reverse(); a.Append(9); a.Prepend(0);
foreach (var (index, item) in products.Index()) { }   // .NET 9

Custom equality for Distinct/Union/Intersect/Except on classes: implement IEquatable<T> + GetHashCode, pass an IEqualityComparer<T>, use records, or use the *By variants.