{"record":{"id":"337458dea7d8193e","repo":"dotnet/reactive","slug":"no-matching-elements","errorCode":null,"errorMessage":"NO_MATCHING_ELEMENTS","messagePattern":"NO_MATCHING_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/SingleAsync.cs","lineNumber":140,"sourceCode":"                            catch (Exception e)\n                            {\n                                ForwardOnError(e);\n                            }\n                            return;\n                        }\n\n                        _value = value;\n                        _seenValue = true;\n                    }\n                }\n\n                public override void OnCompleted()\n                {\n                    if (!_seenValue)\n                    {\n                        try\n                        {\n                            throw new InvalidOperationException(Strings_Linq.NO_MATCHING_ELEMENTS);\n                        }\n                        catch (Exception e)\n                        {\n                            ForwardOnError(e);\n                        }\n                    }\n                    else\n                    {\n                        ForwardOnNext(_value!);\n                        ForwardOnCompleted();\n                    }\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":122,"sourceCodeEnd":157,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/SingleAsync.cs#L122-L157","documentation":"SingleAsync throws InvalidOperationException(NO_MATCHING_ELEMENTS) from its OnCompleted observer callback when the source completes without ever emitting a value. Single* operators require exactly one element; an empty sequence violates that contract, so the library signals the failure via OnError instead of returning a default. It is raised asynchronously during the terminal OnCompleted notification, not at subscription time.","triggerScenarios":"Calling Observable.SingleAsync(source) (or SingleAsync with a predicate) on a source that emits zero OnNext notifications before OnCompleted. The throw happens inside SingleAsync's observer.OnCompleted override at SingleAsync.cs:140 when _seenValue is still false.","commonSituations":"Subscribing to an empty observable (e.g. an empty array converted with ToObservable, a filtered stream that matches nothing, or a backend query returning no rows) and then expecting a single value; race conditions where an upstream filter becomes stricter and the stream can legitimately go empty.","solutions":["Use SingleOrDefaultAsync instead of SingleAsync if an empty source is acceptable (it yields default(TSource)).","Guard the sequence with .DefaultIfEmpty(...) before applying SingleAsync so OnCompleted always follows at least one value.","Ensure upstream filtering cannot eliminate all elements, or handle the OnError(InvalidOperationException) explicitly in Subscribe.","If the stream should never be empty, fix the producer that publishes zero elements."],"exampleFix":"// before\nvar value = await source.SingleAsync(); // throws when source is empty\n\n// after\nvar value = await source.SingleOrDefaultAsync(); // default(TSource) when empty\n// or\nvar value = await source.DefaultIfEmpty(fallback).SingleAsync();","handlingStrategy":"validation","validationCode":"// Only call SingleAsync when the sequence is guaranteed non-empty\n// e.g. check emptiness first:\nbool hasAny = await source.Any(); // .Any() returns IObservable<bool>\nif (!hasAny) { /* use default / report empty */ }\nelse var value = await source.SingleAsync();","typeGuard":"static bool HasExactlyOne<T>(IList<T> buffered) => buffered.Count == 1;","tryCatchPattern":"source.SingleAsync().Subscribe(\n    value => { /* use value */ },\n    ex => { if (ex is InvalidOperationException) { /* empty sequence path */ } else throw ex; });","preventionTips":["Prefer SingleOrDefaultAsync whenever an empty source is plausible.","Apply .DefaultIfEmpty(fallback) before SingleAsync for streams that may go empty.","Assert producer contracts: a stream declared 'exactly one value' must not have filters added downstream that can empty it.","Write a unit test feeding Observable.Empty<T>() through every Single* usage."],"tags":["rx","reactive-extensions","single-operator","empty-sequence"],"backgroundTag":"empty-result-set","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}