Microsoft has provided the LINQ Methods under the System.LINQ namespace. It is a very powerful tool to work upon collections/Lists. Let us quickly understand the syntax and concepts for implementing the Distinct method of LINQ under different circumstances.
Below is the list of different ways that we will discuss to fetch Distinct Objects.
- Using Distinct method
- Using GroupBy method
- Using IEqualityComparer Interface
- Using C# 9.0 Record Types
Let us Consider below example of collection using which we will understand LINQ Distinct and group by
public class Order
{
public int orderId;
public long amount;
public string location;
}
static void Main(string[] args)
{
List<Order> orders = new List<Order>() {
new Order{ orderId = 1, amount = 1000, location = "TX"},
new Order{ orderId = 2, amount = 2000, location = "TX"},
new Order{ orderId = 3, amount = 2000, location = "TX"},
new Order{ orderId = 4, amount = 1000, location = "TX"}
};
}
Finding distinct values based on a property/field
1. Using Distinct Method
var uniqueOrders = orders.Select(x => new { x.location }).Distinct();
If we just use orders.Distinct(), it will not work since the the compiler doesn’t know how to differentiate between different objects, so it compares by hash code of objects, so all the objects are distinct by default. Hence we create an anonymous type, so that it will compare each property separately.
the above query returns the below result

2. Using GroupBy Method
To find distinct values using query syntax, GroupBy operator of LINQ is used as below:
var uniqueOrders = orders.GroupBy(x => new { x.location }).Select(x => x);
3. Using IEqualityComparer Interface
We create a class implementing IEqualityComparer to provide the Distinct method the logic on the basis of which it can to the comparison.
public class OrderComparer : IEqualityComparer<Order>
{
public bool Equals(Order x, Order y)
{
if (Object.ReferenceEquals(x, y))
return true;
if (Object.ReferenceEquals(x, null)
|| Object.ReferenceEquals(y, null))
return false;
return x.location == y.location;
}
public int GetHashCode(Order obj)
{
if (Object.ReferenceEquals(obj, null))
return 0;
int locationHash = obj.location == null ? 0 :
obj.location.GetHashCode();
return locationHash;
}
}
Now, the distinct method can be used as below:
var uniqueOrders = orders.Distinct(new OrderComparer());
4. Using C# 9.0 Record Types
The record types are introduced in C# 9.0 version. Remember that C# 9.0 is only supported in .NET 5 . So remember to update the .NET version too. Record types have pre defined Equality on the basis of values, i.e. Value Equality. So if all the properties are equal, then 2 objects are considered as equal. We can use this feature with LINQ’s Distinct method to achieve uniqueness.
In below code, instead of class we create record type for Order and then use distinct on the same.
public record Order (
long amount,
string location
);
public static void Main()
{
List<Order> orders = new List<Order>() {
new( 1000, "TX"),
new( 2000, "TX"),
new( 1000, "TX"),
new( 1000, "TX"),
};
var uniqueOrders = orders.Distinct();
}
In the above examples, 2 orders are Unique, hence we get 2 records under the uniqueRecords variable as below:

Record types are a good choice if distinct records are to be found based on multiple/all properties. Below are the examples of finding distinct on basis of multiple properties using our previous methods.
Below is the example for finding distinct on the basis of multiple properties/fields
1. Using Distinct method
var uniqueOrders = orders.Select(x => new { x.location, x.amount }).Distinct();
2. Using GroupBy Method
var uniqueOrders = orders.GroupBy(x => new { x.location, x.amount }).Select(x => x);
3. Using IEqualityComparer Interface
public class OrderComparer : IEqualityComparer<Order>
{
public bool Equals(Order x, Order y)
{
if (Object.ReferenceEquals(x, y))
return true;
if (Object.ReferenceEquals(x, null)
|| Object.ReferenceEquals(y, null))
return false;
return x.location == y.location
&& x.amount == y.amount;
}
public int GetHashCode(Order obj)
{
if (Object.ReferenceEquals(obj, null))
return 0;
int locationHash = obj.location == null ?
0 : obj.location.GetHashCode();
int amountHash = obj.amount.GetHashCode();
return locationHash ^ amountHash;
}
}
To Conclude
Microsoft Provides LINQ which is a very powerful tool to manipulate collections. We discussed different methods for the same. Please let us know your suggestions into the comment box.