dotnet/reactive · error · InvalidOperationException

Strings_Linq.NO_MATCHING_ELEMENTS

Error message

Strings_Linq.NO_MATCHING_ELEMENTS

What it means

FirstAsync with a predicate throws InvalidOperationException with Strings_Linq.NO_MATCHING_ELEMENTS when the source completes and no element satisfied the predicate (_found remains false). Forwarded to the observer via OnError.

Solutions

  1. Use FirstOrDefaultAsync(predicate) to get default(T) instead of an error when nothing matches
  2. Catch InvalidOperationException in OnError and fall back
  3. Double-check the predicate logic against the actual data

Example fix

// before
source.FirstAsync(x => x.IsValid).Subscribe(...);
// after
source.FirstOrDefaultAsync(x => x.IsValid).Subscribe(x => ...);
Defensive patterns

Strategy: try-catch

Try / catch

source.FirstAsync(pred).Subscribe(
    x => Console.WriteLine(x),
    ex => { if (ex is InvalidOperationException) Console.WriteLine(default); else throw ex; });

Prevention

When it happens

Trigger: Subscribing to source.FirstAsync(x => predicate(x)) where no emitted element matches before completion.

Common situations: Waiting for a matching status event that never arrives; filtering a stream by a condition that is never true due to a config or data-format mistake.

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/73ff06c4ebca9db1. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/FirstAsync.cs:109

                        ForwardOnError(ex);
                        return;
                    }

                    if (b)
                    {
                        _found = true;
                        ForwardOnNext(value);
                        ForwardOnCompleted();
                    }
                }

                public override void OnCompleted()
                {
                    if (!_found)
                    {
                        try
                        {
                            throw new InvalidOperationException(Strings_Linq.NO_MATCHING_ELEMENTS);
                        }
                        catch (Exception e)
                        {
                            ForwardOnError(e);
                        }
                    }
                }
            }
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)