System.InvalidOperationException: Sequence contains no elements

.NET · updated 2026-11-13

Thrown by LINQ's First, Last, Single, Min, Max and Average when the sequence is empty (and by Entity Framework's FirstAsync and SingleAsync when the query returns no rows). Single also throws "Sequence contains more than one element" when there are two or more.

Unhandled exception. System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Program.<Main>$(String[] args) in /app/Program.cs:line 2

Quick fix

Find the First/Single/Last/Max call in the first frame from your code. If an empty result is possible, switch to FirstOrDefault (or SingleOrDefault) and handle null, or check Any() first. If it should never be empty, the bug is upstream: the filter, the data or a missing seed row, so fail with a message that names what was missing.

  1. Skip the System.Linq frames: the first frame from your code is the call that hit an empty sequence.
  2. Decide whether empty is a normal result (no orders yet, no match for a filter).
  3. Normal: use FirstOrDefault/SingleOrDefault and handle null, or DefaultIfEmpty for Max/Min.
  4. Not normal: check the filter and the data, and throw a clear exception (or return 404) that names the missing record.
csharp
var latest = orders
    .Where(o => o.Paid)
    .OrderByDescending(o => o.At)
    .FirstOrDefault();

if (latest is null)
    return Results.NotFound("no paid orders yet");

// Max on a possibly empty sequence
var newest = orders.Select(o => (DateTime?)o.At).Max(); // null when empty
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/dotnet-sequence-contains-no-elements.md and fix this error in this project.

Why it happens

The query has no matches for this user or tenant

It works with seeded development data and fails for a new account with no rows, or after a filter that excludes everything.

Max, Min or Average on an empty collection

These throw on an empty sequence of non-nullable values. Projecting to a nullable type returns null instead.

Single where duplicates exist

Single and SingleOrDefault throw "Sequence contains more than one element" when two rows match, often after a missing unique index.

A config or lookup table is missing a row

Code that reads settings.First(s => s.Key == "x") breaks in an environment where that row was never inserted.

Other fixes

  • In Entity Framework, use FirstOrDefaultAsync and return 404 for a missing record from an API.
  • Add a unique index where you use Single, so duplicates fail on insert instead of on read.

Same error, other wording

  • System.InvalidOperationException: Sequence contains more than one element
  • System.InvalidOperationException: Sequence contains no matching element

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the "Sequence contains no elements" errors in production, find the First/Single call in the latest one's stack, and decide with the data whether empty is valid (FirstOrDefault plus handling) or a bug upstream.

Setup for .NET and ASP.NET Core, then have your agent fix and resolve it.

Related errors

More in Undefined, None and type errors

References: .NET API: Enumerable.First, .NET API: Enumerable.FirstOrDefault

Spotted a mistake? Email hello@greatwork.company. About Runheed.