{"record":{"id":"13cbec9de5fa132b","repo":"dotnet/reactive","slug":"strings-linq-no-elements-min","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/Min.cs","lineNumber":83,"sourceCode":"                else\n                {\n                    _hasValue = true;\n                    _lastValue = value;\n                }\n            }\n\n            public override void OnError(Exception error)\n            {\n                ForwardOnError(error);\n            }\n\n            public override void OnCompleted()\n            {\n                if (!_hasValue)\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                    ForwardOnNext(_lastValue!);\n                    ForwardOnCompleted();\n                }\n            }\n        }\n\n        private sealed class Null : _\n        {\n            private TSource? _lastValue;\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/Min.cs#L65-L101","documentation":"The Min operator's observer (Min.cs ~83) throws InvalidOperationException(Strings_Linq.NO_ELEMENTS) when OnCompleted arrives and no element was ever observed (_hasValue == false). As with Max, the minimum of an empty sequence is undefined, so Rx matches LINQ-to-Objects and fails explicitly instead of returning a default.","triggerScenarios":"Subscribing to Observable.Min over a source that completes without emitting: Observable.Empty<int>().Min(), a Where clause matching nothing, or a window/group with zero events feeding Min.","commonSituations":"Finding the lowest price/temperature in an empty result set; empty queues drained before Min is computed; filter regressions that exclude all data.","solutions":["Use source.DefaultIfEmpty(defaultValue).Min() for empty-tolerant semantics.","Check source.Any() first and provide a fallback when false.","Catch InvalidOperationException around the subscription and handle 'no data'.","Seed the sequence with StartWith(baseline) so Min always has a candidate."],"exampleFix":"// before\nvar min = await prices.Min(); // throws if prices is empty\n\n// after\nvar min = await prices\n    .DefaultIfEmpty(decimal.MaxValue)\n    .Min();","handlingStrategy":"validation","validationCode":"bool hasAny = await prices.Any();\nif (!hasAny) return decimal.MaxValue;\nvar min = await prices.Min();","typeGuard":"static async Task<bool> IsEmptyAsync<T>(this IObservable<T> source) =>\n    !await source.Any();","tryCatchPattern":"try\n{\n    return await prices.Min();\n}\ncatch (InvalidOperationException ex) when (ex.Message == Strings_Linq.NO_ELEMENTS)\n{\n    return decimal.MaxValue; // identity for Min\n}","preventionTips":["Use DefaultIfEmpty with the Min-identity value (e.g. decimal.MaxValue) for empty tolerance.","Check Any() before Min in interactive or user-triggered queries.","Keep aggregation operators away from sources known to complete immediately empty (e.g. Observable.Empty).","Add empty-input cases to aggregation unit tests."],"tags":["reactive","linq","empty-sequence","aggregation","system-reactive"],"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"}