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).
Sequence contains no elementsSequence contains more than one elementSequence contains no matching element 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
- Use FirstOrDefault/SingleOrDefault and null-check
- Use
DefaultIfEmpty()before aggregates, orMax()on nullable projections - Use Single only when uniqueness is guaranteed by a constraint
Detailed fix by platform
.NET
- 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
- Method — First/Single/Max?
- Data — Should empty be possible?
- 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:
Similar errors
- Index was out of range ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
- NoSuchElementException java.util.NoSuchElementException: No value present
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
Most viewed in .NET
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- Unable to cast object of type System.InvalidCastException: Unable to cast object of type 'X' to type 'Y'
- Cannot access a disposed context instance ObjectDisposedException: Cannot access a disposed context instance
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 12 Oct 2026