{"record":{"id":"879e82bd8bfd789d","repo":"dotnet/reactive","slug":"strings-linq-no-elements","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/Aggregate.cs","lineNumber":67,"sourceCode":"                        _accumulation = default;\n                        ForwardOnError(exception);\n                    }\n                }\n            }\n\n            public override void OnError(Exception error)\n            {\n                _accumulation = default;\n                ForwardOnError(error);\n            }\n\n            public override void OnCompleted()\n            {\n                if (!_hasAccumulation)\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 accumulation = _accumulation!;\n                    _accumulation = default;\n                    ForwardOnNext(accumulation);\n                    ForwardOnCompleted();\n                }\n            }\n        }\n    }\n\n    internal sealed class Aggregate<TSource, TAccumulate> : Producer<TAccumulate, Aggregate<TSource, TAccumulate>._>","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/Aggregate.cs#L49-L85","documentation":"Observable.Aggregate throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source sequence completes without emitting any elements. Without a seed, Aggregate has no accumulation to produce, so OnCompleted forwards an error instead of a result (mirroring LINQ-to-Objects Aggregate behavior).","triggerScenarios":"Subscribing to Observable.Aggregate(source, accumulator) (no seed overload) where the source completes empty, e.g. an empty array, a filtered-out stream, or a never-pushed subject that completes.","commonSituations":"Aggregating query results that returned nothing; Sum-like folds over streams whose filter removed all items; cold sources like empty lists passed to Aggregate without a seed.","solutions":["Use the seed overload: Aggregate(source, seed, accumulator) so an empty sequence yields the seed","Handle the error in the observer's OnError or Catch: source.Aggregate(acc).Catch(Observable.Return(seed))","Ensure the source actually emits elements (check upstream filters/conditions)"],"exampleFix":"// before\nsource.Aggregate((acc, x) => acc + x).Subscribe(...); // throws if empty\n// after\nsource.Aggregate(0, (acc, x) => acc + x).Subscribe(...);","handlingStrategy":"try-catch","validationCode":"bool hasAny = null; source.Materialize().Where(n => n.Kind == NotificationKind.OnNext).Take(1).Subscribe(...); // probe if emptiness matters","typeGuard":null,"tryCatchPattern":"source.Aggregate((acc, x) => acc + x)\n    .Catch<TSource>((InvalidOperationException e) => Observable.Return(seed))\n    .Subscribe(onNext, onError, onCompleted);","preventionTips":["Prefer the seed overload of Aggregate for sources that may be empty","Treat unseeded Aggregate like LINQ's Aggregate: only use when non-emptiness is guaranteed","Add a Count() check or Any() check upstream when emptiness is possible"],"tags":["empty-sequence","aggregate","invalidoperation","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-15T23:17:13.987Z"}