dotnet/reactive · error · InvalidOperationException

Strings_Linq.NO_ELEMENTS

Error message

Strings_Linq.NO_ELEMENTS

What it means

An Average operator (integer variant) throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source completes without emitting any value, because the average of an empty sequence is undefined. The operator forwards the error on OnCompleted instead of producing a result.

Solutions

  1. Use the nullable overload Observable.Average(source.Select(x => (int?)x)) which yields null for empty sequences
  2. Catch the error: source.Average().Catch(Observable.Return(0.0)) or supply a default via Materialize/Dematerialize
  3. Ensure the source emits at least one element before completing

Example fix

// before
source.Average().Subscribe(avg => ...); // throws on empty
// after
source.Select(x => (int?)x).Average().Subscribe(avg => HandleAvg(avg ?? 0));
Defensive patterns

Strategy: try-catch

Validate before calling

bool isEmpty = true; source.Subscribe(_ => isEmpty = false, () => { if (isEmpty) avgSubject.OnNext(0); });

Try / catch

source.Average()
    .Catch<double>(ex => ex is InvalidOperationException ? Observable.Return(0.0) : Observable.Throw<double>(ex))
    .Subscribe(avg => ...);

Prevention

When it happens

Trigger: Subscribing to Observable.Average over an empty source: an empty int/long array, a stream fully filtered out, or a completed subject with no OnNext.

Common situations: Averaging sensor or metrics streams that produced no samples; averaging query results that came back empty; tests that forgot to push values before completion.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/8c85dc22d8e73172. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/Average.cs:59

                }
                catch (Exception ex)
                {
                    ForwardOnError(ex);
                }
            }

            public override void OnCompleted()
            {
                if (_count > 0)
                {
                    ForwardOnNext(_sum / _count);
                    ForwardOnCompleted();
                }
                else
                {
                    try
                    {
                        throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
                    }
                    catch (Exception e)
                    {
                        ForwardOnError(e);
                    }
                }
            }
        }
    }

    internal sealed class AverageSingle : Producer<float, AverageSingle._>
    {
        private readonly IObservable<float> _source;

        public AverageSingle(IObservable<float> source)
        {
            _source = source;
        }

View on GitHub (pinned to 94b5d5ab91)