# [LINQ Operators] Intersect

The “Intersect” operator produces the set intersection of two sequences. You can provide the custom comparer “IEqualityComparer<T>”.

## 1. Sample Data Object

To test examples, you need to include the <data classes> in your project.

## 2. “Intersect” operator

• Deferred operator
• Purpose: Set

## 3. Prototypes

```public static IEnumerable<T> Intersect<T>(
this IEnumerable<T> first,
IEnumerable<T> second)

public static IEnumerable<T> Intersect<T>(
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer)
```

The “Intersect()” operator produces the set intersection of two sequences by using the default equality comparer or the custom comparer.

## 4. Example 1 (Standard Operator)

```int[] numbers1 = {1,2,3,4,5};
int[] numbers2 = {7,6,5,4,3};

var intersectNumbers = numbers1.Intersect(numbers2);
foreach (var num in intersectNumbers)
{
Console.WriteLine(\$"{num}"); // 3,4,5
}
```

## 5. Example 2 (Query Expression)

There’s no corresponding query expression for the “Intersect()” operator.

## 6. Example 3 (Standard Operator) – with a custom comparer

```public class MyNumberEquality : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
return (x % 10 == y % 10);
}
public int GetHashCode(int obj)
{
return (obj % 10).GetHashCode();
}
}
```

```int[] numbers1 = { 11, 12, 13, 14, 15 };
int[] numbers2 = { 23, 24, 25, 26, 27 };

var comparer = new MyNumberEquality();

var intersectNumbers = numbers1.Intersect(numbers2, comparer);
foreach (var num in intersectNumbers)
{
Console.WriteLine(\$"{num}"); // 13,14,15
}
```