{"record":{"id":"7ebf59b3197c07a5","repo":"dotnet/reactive","slug":"strings-linq-no-matching-elements-lastasync","errorCode":null,"errorMessage":"Strings_Linq.NO_MATCHING_ELEMENTS","messagePattern":"Strings_Linq\\.NO_MATCHING_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs","lineNumber":130,"sourceCode":"                    {\n                        _value = value;\n                        _seenValue = true;\n                    }\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_MATCHING_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}","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs#L112-L148","documentation":"LastAsync with a predicate throws InvalidOperationException with Strings_Linq.NO_MATCHING_ELEMENTS when the source completes and no emitted element matched the predicate (_seenValue stays false). Forwarded via OnError.","triggerScenarios":"Subscribing to source.LastAsync(x => predicate(x)) where no element satisfies the predicate before completion.","commonSituations":"Finding the last matching log entry in a stream where the condition never occurs; filtering by a state change that never happened.","solutions":["Use LastOrDefaultAsync(predicate) to get default(T) instead of an error","Catch InvalidOperationException in OnError and fall back","Verify the predicate against the emitted data"],"exampleFix":"// before\nsource.LastAsync(x => x.IsFinal).Subscribe(...);\n// after\nsource.LastOrDefaultAsync(x => x.IsFinal).Subscribe(x => ...);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"source.LastAsync(pred).Subscribe(\n    x => Console.WriteLine(x),\n    ex => { if (ex is InvalidOperationException) Console.WriteLine(default); else throw ex; });","preventionTips":["Prefer LastOrDefaultAsync(predicate)","Verify predicates match real emitted data","Always provide an OnError handler"],"tags":["reactive","linq","last","predicate","no-match"],"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"}