dotnet/reactive · error · InvalidOperationException
NO_ELEMENTS
Error message
NO_ELEMENTS
What it means
SingleAsync also requires at least one element: if OnCompleted arrives with no value ever seen, it throws InvalidOperationException(Strings_Linq.NO_ELEMENTS) forwarded via OnError. This matches LINQ Single() throwing on an empty sequence.
Solutions
- Guarantee the source emits exactly one element before SingleAsync.
- Use SingleOrDefaultAsync when zero elements is an expected, acceptable outcome.
- Catch InvalidOperationException in OnError and supply a default value.
Example fix
// before var user = await users.Where(u => u.Id == id).SingleAsync(); // after var user = await users.Where(u => u.Id == id).SingleOrDefaultAsync();
Defensive patterns
Strategy: fallback
Try / catch
try { var v = await source.SingleAsync(); }
catch (InvalidOperationException) { var v = default(TSource); } Prevention
- Use SingleOrDefaultAsync when zero elements is acceptable.
- Check filter predicates for over-restriction that matches nothing.
- Verify source data exists before subscribing to single-element queries.
When it happens
Trigger: source.SingleAsync() on a stream that completes without emitting any OnNext (e.g. Observable.Empty<T>().SingleAsync()).
Common situations: Filtering with a predicate that matches nothing; querying an entity by id that was deleted; subscribing before data is written and the source closes.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- MORE_THAN_ONE_ELEMENT
- Source sequence doesn't contain any elements.
- Element no longer available in the buffer.
- Strings_Linq.NO_ELEMENTS
- Strings_Linq.NO_ELEMENTS
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/eef383c3280587c1.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/SingleAsync.cs:57
}
catch (Exception e)
{
ForwardOnError(e);
}
return;
}
_value = value;
_seenValue = true;
}
public override void OnCompleted()
{
if (!_seenValue)
{
try
{
throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
}
catch (Exception e)
{
ForwardOnError(e);
}
}
else
{
ForwardOnNext(_value!);
ForwardOnCompleted();
}
}
}
}
internal sealed class Predicate : Producer<TSource, Predicate._>
{
private readonly IObservable<TSource> _source;View on GitHub (pinned to 94b5d5ab91)