dotnet/reactive · error · InvalidOperationException

Strings_Linq.NO_ELEMENTS

Error message

Strings_Linq.NO_ELEMENTS

What it means

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.

Solutions

  1. Use source.DefaultIfEmpty(defaultValue).Min() for empty-tolerant semantics.
  2. Check source.Any() first and provide a fallback when false.
  3. Catch InvalidOperationException around the subscription and handle 'no data'.
  4. Seed the sequence with StartWith(baseline) so Min always has a candidate.

Example fix

// before
var min = await prices.Min(); // throws if prices is empty

// after
var min = await prices
    .DefaultIfEmpty(decimal.MaxValue)
    .Min();
Defensive patterns

Strategy: validation

Validate before calling

bool hasAny = await prices.Any();
if (!hasAny) return decimal.MaxValue;
var min = await prices.Min();

Type guard

static async Task<bool> IsEmptyAsync<T>(this IObservable<T> source) =>
    !await source.Any();

Try / catch

try
{
    return await prices.Min();
}
catch (InvalidOperationException ex) when (ex.Message == Strings_Linq.NO_ELEMENTS)
{
    return decimal.MaxValue; // identity for Min
}

Prevention

When it happens

Trigger: 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.

Common situations: Finding the lowest price/temperature in an empty result set; empty queues drained before Min is computed; filter regressions that exclude all data.

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/13cbec9de5fa132b. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/Min.cs:83

                else
                {
                    _hasValue = true;
                    _lastValue = value;
                }
            }

            public override void OnError(Exception error)
            {
                ForwardOnError(error);
            }

            public override void OnCompleted()
            {
                if (!_hasValue)
                {
                    try
                    {
                        throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
                    }
                    catch (Exception e)
                    {
                        ForwardOnError(e);
                    }
                }
                else
                {
                    ForwardOnNext(_lastValue!);
                    ForwardOnCompleted();
                }
            }
        }

        private sealed class Null : _
        {
            private TSource? _lastValue;

View on GitHub (pinned to 94b5d5ab91)