Sequence contains more than one element
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The "Sequence contains more than one element" exception is one of the most common runtime errors in C# LINQ code. It is thrown by the Single() and SingleOrDefault() methods when they find more than one element matching the query condition. This article explains why it happens, how to fix it, and which LINQ method to use based on your actual requirements.
Why This Exception Occurs
The Single() method expresses a strong assertion: "there is exactly one element in this sequence that matches my condition." If the sequence has zero elements or more than one, the assertion is violated and LINQ throws an InvalidOperationException.
There are two values of 2 in the list, so Single() cannot determine which one to return. Rather than silently picking one (which would mask a data integrity issue), it throws an exception.
Single() vs. SingleOrDefault() vs. First() vs. FirstOrDefault()
Understanding the four related methods is essential for choosing the right one:
| Method | 0 elements | 1 element | 2+ elements |
Single() | Throws | Returns it | Throws |
SingleOrDefault() | Returns default | Returns it | Throws |
First() | Throws | Returns it | Returns first |
FirstOrDefault() | Returns default | Returns it | Returns first |
The key difference: Single methods throw when there are multiple matches. First methods simply return the first match and ignore the rest.
Common Scenarios That Cause This Error
Duplicate Data
The most frequent cause is unexpected duplicate records in a database:
Missing or Overly Broad Filter
Sometimes the predicate is not specific enough to narrow down to one result:
Querying Without a Predicate
Calling Single() on a collection without a predicate asserts that the entire collection has exactly one element:
How to Fix It
Option 1: Use First() or FirstOrDefault() If Duplicates Are Acceptable
If having multiple matches is normal and you just want any one of them:
Use FirstOrDefault() if the sequence might be empty and you want null instead of an exception:
Option 2: Fix the Data If Duplicates Are a Bug
If duplicates indicate a data integrity problem, the right fix is at the data level. Add a unique constraint to prevent future duplicates:
Then clean up existing duplicates before using Single() again.
Option 3: Add a More Specific Predicate
Narrow your query so it truly returns one result:
Option 4: Use Where() and Handle Multiple Results Explicitly
If you want full control, use Where() and handle the count yourself:
SingleOrDefault() Still Throws for Multiple Elements
A common misconception is that SingleOrDefault() is the "safe" version of Single(). It only handles the empty-sequence case by returning default (typically null). It still throws for multiple elements:
If you want safety from both empty sequences and multiple matches, FirstOrDefault() is the method you need.
Common Pitfalls
- Using
Single()for lookups that should useFirst(): If your business logic does not require uniqueness, do not useSingle(). It adds a constraint that the data may not satisfy. - Assuming
SingleOrDefault()handles duplicates: It does not. You still get the exception for multiple matches. - Not ordering when using
First(): WithoutOrderBy(),First()returns whichever element the database or LINQ provider happens to return first, which can vary between runs. - Clean test data masking the issue: If
Single()works in tests but fails in production, your test data is too clean. Add edge cases with duplicates.
Summary
The "Sequence contains more than one element" exception means your Single() or SingleOrDefault() call found multiple matching elements when it expected at most one. If duplicates are acceptable, switch to First() or FirstOrDefault(). If duplicates are a bug, fix the data and add unique constraints. Always pair First() with OrderBy() for deterministic results. Choose the LINQ method that matches your actual data guarantees, not the one that sounds most convenient.

