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
Intersect two lists in C#
In C#, you can find common elements between two lists using the Intersect() method from LINQ. This method returns an IEnumerable<T> containing elements that appear in both lists, automatically removing duplicates.
Syntax
Following is the syntax for using the Intersect() method −
IEnumerable<T> result = list1.Intersect(list2);
The method can also accept a custom equality comparer −
IEnumerable<T> result = list1.Intersect(list2, comparer);
Using Intersect() with Integer Lists
The most common use case is finding intersection between two integer lists −
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<int> val1 = new List<int> { 25, 30, 40, 60, 80, 95, 110 };
List<int> val2 = new List<int> { 27, 35, 40, 75, 95, 100, 110 };
IEnumerable<int> res = val1.Intersect(val2);
Console.WriteLine("Intersection of both the lists...");
foreach (int a in res) {
Console.WriteLine(a);
}
}
}
The output of the above code is −
Intersection of both the lists... 40 95 110
Using Intersect() with String Lists
The Intersect() method also works with string collections −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> fruits1 = new List<string> { "apple", "banana", "orange", "grape" };
List<string> fruits2 = new List<string> { "banana", "kiwi", "grape", "mango" };
var commonFruits = fruits1.Intersect(fruits2);
Console.WriteLine("Common fruits:");
foreach (string fruit in commonFruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Common fruits: banana grape
Using Intersect() with Custom Objects
For custom objects, you can override Equals() and GetHashCode() methods or use a custom comparer −
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) {
if (obj is Student student) {
return this.Id == student.Id;
}
return false;
}
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 = "John" },
new Student { Id = 2, Name = "Alice" },
new Student { Id = 3, Name = "Bob" }
};
List<Student> class2 = new List<Student> {
new Student { Id = 2, Name = "Alice" },
new Student { Id = 4, Name = "Carol" },
new Student { Id = 3, Name = "Bob" }
};
var commonStudents = class1.Intersect(class2);
Console.WriteLine("Students in both classes:");
foreach (var student in commonStudents) {
Console.WriteLine(student);
}
}
}
The output of the above code is −
Students in both classes: Id: 2, Name: Alice Id: 3, Name: Bob
Key Features
The
Intersect()method automatically removes duplicates from the result.The order of elements in the result follows the order of the first list.
It uses the default equality comparer unless a custom comparer is provided.
The method is part of LINQ and requires
using System.Linq;.
Conclusion
The Intersect() method in C# provides an efficient way to find common elements between two lists. It works with any type of collection and automatically handles duplicate removal, making it a powerful tool for set operations in LINQ.
