Distinct and HashSet04/21/2026
A common pattern in business-as-usual (BAU) applications is that a class in the business logic layer requests a set of entities by their integer IDs from a repository class. Here is one possible implementation of this pattern:
- A method in the business layer receives a list of integers as an argument (
List<int>). - It applies filtering and selection on these integers by creating a new enumerable.
- It extracts the unique values from the filtered and projected numbers.
- It passes the filtered, selected unique numbers to a query method in the repository class.
- This repository method processes the passed in integer enumerable:
- If there aren't any numbers in the enumeration, it returns null (or empty results).
- Otherwise, it copies the numbers into an SQL query and returns the query's result.
In this blog post, I will focus on two possible implementations to produce the unique set of integers:
- Using the
DistinctLINQ extension method. - Using the
ToHashSet<T>method to materialize the result before passing it to the repository class.