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:
You are filtering a single collection.
The filtering condition is based on business logic.
You need to extract a property from records.
You want unique values after filtering.
Real-World Examples
Common scenarios include:
Finding unique customers who purchased a specific product.
Finding unique email addresses from active users.
Extracting distinct categories from filtered inventory.
Finding unique order IDs after applying business rules.
Getting unique locations from filtered datasets.
For example:
var uniqueCustomers = orders
.Where(order => order.ProductCode == "DOTNET-BOOK")
.Select(order => order.CustomerId)
.Distinct();
Here, Where() expresses the filtering rule, Select() extracts the required value, and Distinct() removes duplicates.
Intersect() vs Where() + Distinct()
Aspect | Intersect() | Where() + Distinct() |
|---|---|---|
Primary purpose | Find common values | Filter and deduplicate |
Typical input | Two sequences | One sequence |
Compares collections | Yes | Not directly |
Custom filtering | Not its primary purpose | Yes |
Removes duplicates | Yes | Yes, through |
Best suited for | Finding overlaps | Applying business rules |
Common examples | IDs, roles, permissions, tags | Customers, categories, filtered records |
Can Where() + Distinct() Replace Intersect()?
Sometimes, but it usually makes the intent less clear.
For example, suppose you want to find common values between two collections:
var first = new[] { 1, 2, 3, 4 };
var second = new[] { 3, 4, 5, 6 };
Intersect() expresses the requirement directly:
var common = first.Intersect(second);
The result is:
3
4
You could build equivalent logic with filtering, but it would require explicitly checking whether each value exists in the second collection. Intersect() communicates the intent more clearly.
Important Point About Distinct()
Intersect() already returns distinct results, so adding Distinct() after Intersect() is normally unnecessary.
For example:
var result = first
.Intersect(second)
.Distinct();
can generally be simplified to:
var result = first.Intersect(second);
Distinct() is useful with Where() when the filtering operation can produce duplicate values and you need to remove them.
Choosing the Right LINQ Method
A simple way to remember the difference is:
Need common values from two collections?
↓
Intersect()
Need to filter one collection?
↓
Where()
Need unique values from the result?
↓
Distinct()
These methods can also be combined when the problem requires it.
For example:
var customerIds = orders
.Where(order => order.ProductCode == "DOTNET-BOOK")
.Select(order => order.CustomerId)
.Distinct();
This query has three clearly defined operations:
Where()→ filters the orders.Select()→ extracts customer IDs.Distinct()→ removes duplicate customer IDs.
Common Mistake
A common mistake is choosing Intersect() simply because the expected output needs to be unique.
Uniqueness is only one part of what Intersect() provides. Its primary purpose is to find the set intersection between two sequences.
If you only have one collection and need to apply a condition, Where() is the appropriate starting point.
If you also need unique results, add Distinct().
Conclusion
Intersect() and Where() + Distinct() may sometimes produce similar-looking results, but they represent different operations.
Use Intersect() when you need to find common values between two collections. Use Where() when you need to filter a collection based on a condition, and add Distinct() when duplicate results need to be removed.
Understanding the intent behind each LINQ method makes queries easier to read, maintain, and reason about.
A simple rule to remember is:
Intersect() = find common values.
Where() + Distinct() = filter and return unique values.

Join the conversation! Your thoughts help the community grow.