Sequence contains no elements .NET Origin: Application & API Last updated 12 Oct 2026

InvalidOperationException: Sequence contains no elements / more than one element

LINQ First(), Single(), Last() or Max() ran on an empty sequence (or Single() found several items).

Exact message you may see Sequence contains no elementsSequence contains more than one elementSequence contains no matching element
Seen on: .NET

Meaning

First()/Single() assert that data exists. When “no result” is a valid case, use the …OrDefault() variants and handle null.

Common causes

  • Query returned no rows but code used First()/Single()
  • Single() on data that has duplicates
  • Max()/Min()/Average() on an empty collection

⚡ Quick fix

  1. Use FirstOrDefault/SingleOrDefault and null-check
  2. Use DefaultIfEmpty() before aggregates, or Max() on nullable projections
  3. Use Single only when uniqueness is guaranteed by a constraint

Detailed fix by platform

.NET

  1. Safe patterns:
    csharp
    var user = await db.Users.FirstOrDefaultAsync(u => u.Email == email);
    if (user is null) return NotFound();
    
    decimal? maxTotal = await db.Orders.Where(o => o.UserId == id).MaxAsync(o => (decimal?)o.Total);

How to diagnose

  1. Method — First/Single/Max?
  2. Data — Should empty be possible?
  3. Uniqueness — Duplicates where Single expects one?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Index was out of range — ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection (.NET)Index was out of rangeNoSuchElementException — java.util.NoSuchElementException: No value present (Java)NoSuchElementExceptionNullReferenceException — System.NullReferenceException: Object reference not set to an instance of an object (.NET)NullReferenceExceptionSubquery returns more than 1 row — Subquery returns more than 1 row (MySQL 1242) / more than one row returned by a subquery used as an expression (SQL)Subquery returns more…The LINQ expression could not be translated — EF Core: The LINQ expression 'x' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly (.NET)The LINQ expression c…Sequence …

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.