Skip to content

C# LINQ MinBy — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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

You need the cheapest product, the youngest user, or the shortest name. Min() returns only the value, not the element. You write a loop tracking both the min value and the element, or use Aggregate. MinBy (C# 9+) returns the element with the minimum key value.

Wrong

var cheapest = products[0];
for (int i = 1; i < products.Count; i++)
{
    if (products[i].Price < cheapest.Price)
        cheapest = products[i];
}
// Or:
var cheapest = products.OrderBy(p => p.Price).First();

Output: Works, but the loop is verbose and OrderBy + First is O(n log n) for an O(n) operation.

var cheapest = products.MinBy(p => p.Price);

Output: The Product with the lowest Price. O(n) time, single pass.

MinBy returns the first element with the minimum key if there are multiple:

var scores = new[] { (Name: "Alice", Score: 90), (Name: "Bob", Score: 70), (Name: "Charlie", Score: 70) };
var min = scores.MinBy(s => s.Score);
// (Name: "Bob", Score: 70) — first occurrence

Prevention

  • Use MinBy to find the element with the minimum key value.
  • Use Min when you only need the key value, not the element.
  • Use MaxBy for the element with the maximum key.
  • Use OrderBy + First only when you need a specific tie-breaking order.
  • Use MinBy with IQueryable in EF Core 6+ for server-side evaluation.
  • Use MinBy with nullable keys — null values are considered less than non-null.

Common Mistakes with linq min by

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

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

What is the difference between Min and MinBy?

Min returns the minimum value itself: products.Min(p => p.Price) returns a decimal. MinBy returns the element: products.MinBy(p => p.Price) returns a Product. Use Min for values, MinBy for elements.

Does MinBy work with IQueryable in EF Core?

Yes, in EF Core 6+. It translates to SQL ORDER BY ... LIMIT 1 or similar depending on the provider. Use it for server-side min-by queries to avoid loading all rows into memory.

What happens if the sequence is empty?

MinBy throws InvalidOperationException on an empty sequence. Use MinBy with a nullable source or check Any() first. Alternatively, use DefaultIfEmpty().MinBy(...) to get a default value.

MinBy is used in Durga Antivirus Pro to find the smallest file signature match. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro