Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq Intersect Method
The Intersect() method in C# LINQ is used to find common elements between two collections. It returns a new sequence containing elements that exist in both the source collection and the specified collection, with duplicates automatically removed.
Syntax
Following is the syntax for the Intersect() method −
public static IEnumerable<TSource> Intersect<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)
With a custom equality comparer −
public static IEnumerable<TSource> Intersect<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second,
IEqualityComparer<TSource> comparer
)
Parameters
-
first − The first collection (source sequence).
-
second − The collection to compare with the first collection.
-
comparer − Optional. An
IEqualityComparerto compare values.
Return Value
Returns an IEnumerable<TSource> containing the common elements from both collections, with duplicates removed.
Using Intersect() with Integer Arrays
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
int[] val1 = { 15, 20, 40, 60, 75, 90 };
int[] val2 = { 17, 25, 35, 55, 75, 90 };
IEnumerable<int> res = val1.Intersect(val2);
Console.WriteLine("Array 1: " + string.Join(", ", val1));
Console.WriteLine("Array 2: " + string.Join(", ", val2));
Console.WriteLine("Intersection of both arrays:");
foreach (int a in res) {
Console.WriteLine(a);
}
}
}
The output of the above code is −
Array 1: 15, 20, 40, 60, 75, 90 Array 2: 17, 25, 35, 55, 75, 90 Intersection of both arrays: 75 90
Using Intersect() with String Collections
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> fruits1 = new List<string> { "Apple", "Banana", "Orange", "Grapes" };
List<string> fruits2 = new List<string> { "Banana", "Mango", "Orange", "Kiwi" };
var commonFruits = fruits1.Intersect(fruits2);
Console.WriteLine("List 1: " + string.Join(", ", fruits1));
Console.WriteLine("List 2: " + string.Join(", ", fruits2));
Console.WriteLine("Common fruits: " + string.Join(", ", commonFruits));
}
}
The output of the above code is −
List 1: Apple, Banana, Orange, Grapes List 2: Banana, Mango, Orange, Kiwi Common fruits: Banana, Orange
Using Intersect() with Custom Objects
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Student {
public int Id { get; set; }
public string Name { get; set; }
public override bool Equals(object obj) {
return obj is Student student && Id == student.Id;
}
public override int GetHashCode() {
return Id.GetHashCode();
}
public override string ToString() {
return $"Id: {Id}, Name: {Name}";
}
}
class Program {
static void Main() {
List<Student> class1 = new List<Student> {
new Student { Id = 1, Name = "Alice" },
new Student { Id = 2, Name = "Bob" },
new Student { Id = 3, Name = "Charlie" }
};
List<Student> class2 = new List<Student> {
new Student { Id = 2, Name = "Bob" },
new Student { Id = 3, Name = "Charlie" },
new Student { Id = 4, Name = "David" }
};
var commonStudents = class1.Intersect(class2);
Console.WriteLine("Common students:");
foreach (var student in commonStudents) {
Console.WriteLine(student);
}
}
}
The output of the above code is −
Common students: Id: 2, Name: Bob Id: 3, Name: Charlie
Key Features
-
Removes duplicates − The result contains unique elements only.
-
Preserves order − Elements appear in the same order as in the first collection.
-
Deferred execution − The operation is not performed until the result is enumerated.
-
Custom equality − Can use custom
IEqualityComparerfor complex objects.
Conclusion
The LINQ Intersect() method efficiently finds common elements between two collections, automatically removing duplicates and preserving the order from the first collection. It works with any type that implements proper equality comparison and supports custom comparers for complex scenarios.
