What are union, intersect and except operators in Linq C#?

LINQ set operators in C# allow you to perform set-based operations on collections. The three primary set operators are Union, Intersect, and Except, which enable you to combine, find common elements, and identify differences between sequences.

These operators work with any IEnumerable<T> collections and return distinct results by default, making them useful for data manipulation and filtering operations.

Syntax

Following is the syntax for the three LINQ set operators −

// Union - combines two sequences and removes duplicates
var result = sequence1.Union(sequence2);

// Intersect - returns common elements from both sequences
var result = sequence1.Intersect(sequence2);

// Except - returns elements from first sequence not in second
var result = sequence1.Except(sequence2);

Union Operator

The Union operator combines multiple collections into a single collection and returns a resultant collection with unique elements. It automatically removes duplicates from the combined result.

Union Operation Set A {1, 2, 3, 4} Set B {2, 4, 7} ? Union Result: {1, 2, 3, 4, 7}

Intersect Operator

The Intersect operator returns sequence elements which are common in both the input sequences. It finds the intersection of two collections.

Intersect Operation Set A {1, 2, 3, 4} Set B {2, 4, 7} ? Intersect Result: {2, 4}

Except Operator

The Except operator returns sequence elements from the first input sequence that are not present in the second input sequence. It performs a set difference operation.

Except Operation Set A {1, 2, 3, 4} Set B {2, 4, 7} ? Except Result: {1, 3}

Example

The following example demonstrates all three LINQ set operators using integer arrays −

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] count1 = { 1, 2, 3, 4 };
        int[] count2 = { 2, 4, 7 };
        
        var resultUnion = count1.Union(count2);
        var resultIntersect = count1.Intersect(count2);
        var resultExcept = count1.Except(count2);
        
        Console.WriteLine("Union");
        foreach (var item in resultUnion) {
            Console.WriteLine(item);
        }
        
        Console.WriteLine("Intersect");
        foreach (var item in resultIntersect) {
            Console.WriteLine(item);
        }
        
        Console.WriteLine("Except");
        foreach (var item in resultExcept) {
            Console.WriteLine(item);
        }
    }
}

The output of the above code is −

Union
1
2
3
4
7
Intersect
2
4
Except
1
3

Working with String Collections

These operators work equally well with string collections and other reference types −

using System;
using System.Linq;

class Program {
    static void Main() {
        string[] fruits1 = { "apple", "banana", "orange", "grape" };
        string[] fruits2 = { "banana", "grape", "mango", "kiwi" };
        
        var union = fruits1.Union(fruits2);
        var intersect = fruits1.Intersect(fruits2);
        var except = fruits1.Except(fruits2);
        
        Console.WriteLine("Union: " + string.Join(", ", union));
        Console.WriteLine("Intersect: " + string.Join(", ", intersect));
        Console.WriteLine("Except: " + string.Join(", ", except));
    }
}

The output of the above code is −

Union: apple, banana, orange, grape, mango, kiwi
Intersect: banana, grape
Except: apple, orange

Comparison of Set Operators

Operator Purpose Result
Union Combines two sequences All unique elements from both sequences
Intersect Finds common elements Elements present in both sequences
Except Finds difference Elements in first sequence but not in second

Conclusion

LINQ set operators provide powerful tools for working with collections in C#. Union combines sequences while removing duplicates, Intersect finds common elements between sequences, and Except identifies elements unique to the first sequence. These operators are essential for data manipulation and filtering operations in LINQ queries.

Updated on: 2026-03-17T07:04:36+05:30

714 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements