{"record":{"id":"8790ec6b380a68a4","repo":"dotnet/reactive","slug":"sequence-contains-no-elements-taskobservableextensions","errorCode":null,"errorMessage":"Sequence contains no elements.","messagePattern":"Sequence contains no elements\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Threading/Tasks/TaskObservableExtensions.cs","lineNumber":557,"sourceCode":"            public override void OnError(Exception error)\n            {\n                _tcs.TrySetException(error);\n\n                _ctr.Dispose(); // no null-check needed (struct)\n                Dispose();\n            }\n\n            public override void OnCompleted()\n            {\n                if (_hasValue)\n                {\n                    _tcs.TrySetResult(_lastValue!);\n                }\n                else\n                {\n                    try\n                    {\n                        throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);\n                    }\n                    catch (Exception e)\n                    {\n                        _tcs.TrySetException(e);\n                    }\n                }\n\n                _ctr.Dispose(); // no null-check needed (struct)\n                Dispose();\n            }\n\n            private void Cancel()\n            {\n                Dispose();\n                _tcs.TrySetCanceled(_ct);\n            }\n        }\n","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Threading/Tasks/TaskObservableExtensions.cs#L539-L575","documentation":"The ToTask/LastAsync observer's OnCompleted throws InvalidOperationException(\"Sequence contains no elements.\") when the observable completes without emitting any value while being converted to a task that expects a Last value. The exception is routed into the returned Task, which becomes faulted.","triggerScenarios":"Calling observable.ToTask() or LastAsync on an observable that completes with zero OnNext emissions.","commonSituations":"Empty source sequences, fully filtered streams, or early-completed streams due to cancellation/dispose before the first element.","solutions":["Use .DefaultIfEmpty(defaultValue).ToTask() so an empty sequence yields a default.","Await the task inside try/catch of InvalidOperationException when empty is acceptable.","Use FirstAsync/FirstOrDefaultAsync semantics instead of Last/ToTask when a single element may not exist.","Fix the upstream source to emit at least one value."],"exampleFix":"// before\nvar result = await source.ToTask();\n// after\nvar result = await source.DefaultIfEmpty(defaultValue).ToTask();","handlingStrategy":"fallback","validationCode":"var isEmpty = await source.IsEmptyAsync();\nvar task = isEmpty ? Task.FromResult(defaultValue) : source.ToTask();","typeGuard":null,"tryCatchPattern":"try { result = await source.ToTask(); }\ncatch (InvalidOperationException) { result = defaultValue; }","preventionTips":["Chain .DefaultIfEmpty(defaultValue) before ToTask.","Check IsEmptyAsync when a sequence may legitimately be empty."],"tags":["empty-sequence","totask","rx"],"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-23T08:17:48.524Z"}