Skip to content

C# LINQ MaxBy — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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

You need the most expensive product, the oldest user, or the longest name. Max() returns only the value, not the element. You write a loop or use OrderByDescending + First. MaxBy (C# 9+) returns the element with the maximum key value in O(n) time.

Wrong

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

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

var mostExpensive = products.MaxBy(p => p.Price);

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

MaxBy returns the first element with the maximum key if there are multiple:

var scores = new[] { (Name: "Alice", Score: 95), (Name: "Bob", Score: 95), (Name: "Charlie", Score: 80) };
var max = scores.MaxBy(s => s.Score);
// (Name: "Alice", Score: 95) — first occurrence

Prevention

  • Use MaxBy to find the element with the maximum key value.
  • Use Max when you only need the key value, not the element.
  • Use MinBy for the element with the minimum key.
  • Use OrderByDescending + First when tie-breaking is important.
  • Use MaxBy with IQueryable in EF Core 6+ for server-side evaluation.
  • Use MaxBy with nullable keys — non-null values are considered greater than null.

Common Mistakes with linq max 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

What is the difference between Max and MaxBy?

Max returns the maximum value itself: products.Max(p => p.Price) returns a decimal. MaxBy returns the element: products.MaxBy(p => p.Price) returns a Product. Use Max for values, MaxBy for elements.

Does MaxBy work with IQueryable in EF Core?

Yes, in EF Core 6+. It translates to SQL ORDER BY ... DESC LIMIT 1 or equivalent. Use it for server-side max-by queries to avoid loading all rows into memory.

What happens if the sequence is empty?

MaxBy throws InvalidOperationException on an empty sequence. Check Any() first, or use DefaultIfEmpty().MaxBy(...) with a default element.

MaxBy is used in Doda Browser to find the most relevant search result by ranking score. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro