Introduction
In this article, we will learn about Intersect() vs Where() + Distinct() in C# LINQ and understand when each approach should be used.
When working with collections in C#, developers often need to find common values between datasets or retrieve unique values after applying filters. LINQ provides several methods for these scenarios, including Intersect(), Where(), and Distinct().
Although these approaches can sometimes produce similar-looking results, they solve different problems. Understanding the difference helps you write clearer and more maintainable LINQ queries.
What Does Intersect() Do?
The LINQ Intersect() method returns the common distinct values that exist in two sequences.
Key Characteristics
Compares two collections.
Returns values that exist in both collections.
Automatically removes duplicate values.
Is useful when finding overlap between datasets.
Example
Suppose an application has a list of user permissions and a list of permissions required for a particular operation.
var userPermissions = new[]
{
"users.read",
"users.write",
"orders.read",
"reports.read"
};
var requiredPermissions = new[]
{
"users.write",
"admin.access"
};
var matchingPermissions =
userPermissions.Intersect(requiredPermissions);
foreach (var permission in matchingPermissions)
{
Console.WriteLine(permission);
}
Output
users.write
users.write is the only permission that appears in both collections.
The important point is that Intersect() is comparing two sequences and returning their common values.
What Does Where() + Distinct() Do?
The combination of Where() and Distinct() is useful when you want to:
Filter a collection using a condition.
Select the required value.
Remove duplicate values from the filtered result.
Unlike Intersect(), this approach normally starts with a single collection.
Example
Consider an order collection containing customer IDs and product codes.
var orders = new[]
{
new { CustomerId = 101, ProductCode = "DOTNET-BOOK" },
new { CustomerId = 102, ProductCode = "JAVA-BOOK" },
new { CustomerId = 101, ProductCode = "DOTNET-BOOK" },
new { CustomerId = 103, ProductCode = "DOTNET-BOOK" }
};
var customerIds = orders
.Where(order => order.ProductCode == "DOTNET-BOOK")
.Select(order => order.CustomerId)
.Distinct();
foreach (var customerId in customerIds)
{
Console.WriteLine(customerId);
}
Output
101
103
The query first filters the orders where ProductCode is DOTNET-BOOK. It then selects the customer IDs and uses Distinct() to remove the duplicate customer ID 101.
The Fundamental Difference
The core difference is what each operation is trying to accomplish.
Intersect()
Intersect() finds the overlap between two sequences.
For example:
Collection A: 1, 2, 3, 4
Collection B: 3, 4, 5, 6
Intersect:
3, 4
Where() + Distinct()
Where() + Distinct() filters one sequence and then removes duplicate results.
For example:
Orders:
101 - DOTNET-BOOK
102 - JAVA-BOOK
101 - DOTNET-BOOK
103 - DOTNET-BOOK
After Where():
101
101
103
After Distinct():
101
103
So, although both can return unique values, their purposes are different.
When Should You Use Intersect()?
Use Intersect() when:
You already have two collections.
You need values that exist in both collections.
Distinct results are sufficient.
You are comparing identifiers, permissions, roles, tags, or other values.
Real-World Examples
Some common scenarios include:
Finding shared user roles.
Finding matching product tags.
Comparing IDs from two systems.
Finding supported file formats between applications.
Checking users who belong to multiple groups.
For example, checking whether users have permissions from a required permission set is naturally expressed using Intersect():
var commonPermissions =
userPermissions.Intersect(requiredPermissions);
The code directly expresses the business requirement: find permissions common to both collections.
When Should You Use Where() + Distinct()?
Use Where() + Distinct() when:

Join the conversation! Your thoughts help the community grow.