{"record":{"id":"aebdb8165bf16f30","repo":"dotnet/reactive","slug":"strings-linq-no-elements-lastasync","errorCode":null,"errorMessage":"Strings_Linq.NO_ELEMENTS","messagePattern":"Strings_Linq\\.NO_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs","lineNumber":51,"sourceCode":"                public override void OnNext(TSource value)\n                {\n                    _value = value;\n                    _seenValue = true;\n                }\n\n                public override void OnError(Exception error)\n                {\n                    _value = default;\n                    ForwardOnError(error);\n                }\n\n                public override void OnCompleted()\n                {\n                    if (!_seenValue)\n                    {\n                        try\n                        {\n                            throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);\n                        }\n                        catch (Exception e)\n                        {\n                            ForwardOnError(e);\n                        }\n                    }\n                    else\n                    {\n                        var value = _value!;\n                        _value = default;\n                        ForwardOnNext(value);\n                        ForwardOnCompleted();\n                    }\n                }\n            }\n        }\n\n        internal sealed class Predicate : Producer<TSource, Predicate._>","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs#L33-L69","documentation":"LastAsync without a predicate throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source completes without emitting any value, since there is no last element. Thrown in OnCompleted when _seenValue is false and forwarded via OnError.","triggerScenarios":"Subscribing to source.LastAsync() where the source completes with zero OnNext calls.","commonSituations":"Taking the latest reading from a sensor stream that produced nothing; last event of a session that was filtered empty.","solutions":["Use LastOrDefaultAsync which emits default(T) instead of erroring on an empty source","Catch InvalidOperationException in OnError and use a fallback","Ensure the source emits at least one value before completion"],"exampleFix":"// before\nsource.LastAsync().Subscribe(x => ...);\n// after\nsource.LastOrDefaultAsync().Subscribe(x => Console.WriteLine(x));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"source.LastAsync().Subscribe(\n    x => Console.WriteLine(x),\n    ex => { if (ex is InvalidOperationException) Console.WriteLine(default); else throw ex; });","preventionTips":["Use LastOrDefaultAsync for possibly-empty sources","Handle OnError when subscribing to LastAsync","Test with empty sources"],"tags":["reactive","linq","last","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"}