Skip to content

C# LINQ DistinctBy — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# LINQ DistinctBy. We cover key concepts, practical examples, and best practices.

You have a list of objects with duplicate Id values. Distinct() uses default equality — it only removes objects that are equal by reference or value. You want to remove duplicates based on a specific property. You write a GroupBy or a custom IEqualityComparer. DistinctBy (C# 9+) does it directly.

Wrong

var unique = users
    .GroupBy(u => u.Id)
    .Select(g => g.First())
    .ToList();

Output: Works. But GroupBy + Select is verbose and allocates unnecessarily.

var unique = users.DistinctBy(u => u.Id).ToList();

Output: Same result, one line. DistinctBy keeps the first element for each unique key.

The key selector can be any expression:

var uniqueByName = users.DistinctBy(u => u.Name.ToLowerInvariant()).ToList();
var uniqueByTuple = orders.DistinctBy(o => (o.CustomerId, o.ProductId)).ToList();

Prevention

  • Use DistinctBy instead of GroupBy + First for deduplication by key.
  • Use DistinctBy with IQueryable for server-side deduplication in EF Core (supported in EF Core 6+).
  • Use DistinctBy with a tuple key for composite deduplication.
  • Use Distinct() when removing full-object duplicates (no key selector needed).
  • Use a custom IEqualityComparer only when DistinctBy does not support the needed comparison logic.
  • Remember that DistinctBy is LINQ to Objects — it does not translate to SQL in all providers.

Common Mistakes with linq distinct by

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Does DistinctBy preserve order?

Yes. DistinctBy returns elements in the order they first appear in the source sequence. The first occurrence of each key is kept; subsequent duplicates are dropped. This is called "stable" deduplication.

What is the difference between DistinctBy and Distinct?

Distinct() uses the type's default equality comparer — it compares the entire object. DistinctBy compares only the key you specify. Use Distinct for full-object dedup; use DistinctBy for property-based dedup.

Can I use DistinctBy with IQueryable in EF Core?

Yes, in EF Core 6+. It translates to SQL DISTINCT ON (column) or ROW_NUMBER() OVER (PARTITION BY column ORDER BY ...) depending on the database provider. Check the generated SQL for your specific provider.

DistinctBy is used in Doda Browser to deduplicate extension search results by extension ID. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro