dotnet/reactive · error · InvalidOperationException

Could not find a matching method with name

Error message

Could not find a matching method with name '{0}' on type '{1}'.

What it means

Thrown by AsyncEnumerableRewriter.FindMethod when the target type DOES have methods with the requested name, but none of their signatures match the supplied argument types or generic type arguments (ArgsMatch fails for every candidate). Unlike the 'no such method' case, this is an overload-resolution failure during expression rewriting.

Solutions

  1. Adjust the arguments so their types exactly match one declared overload (add explicit casts or typed lambda parameters).
  2. Supply explicit generic type arguments to the operator call.
  3. Convert lambdas to the exact Expression/Func form the target overload expects.
  4. If no overload is right for translation, do that operation outside the queryable expression.

Example fix

// before
var q = source.Select(x => (long)x.A); // matches no overload during rewrite
// after
var q = source.Select((Expression<Func<Item, long>>)(x => x.A)); // exact overload signature
Defensive patterns

Strategy: validation

Validate before calling

var matches = targetType.GetMethods().Where(m => m.Name == opName);
bool anyFit = matches.Any(m => m.GetParameters().Length == argCount);
if (!anyFit) throw new InvalidOperationException($"no {opName} overload takes {argCount} args");

Type guard

static bool OverloadExists(Type t, string name, int argCount) =>
    t.GetMethods().Any(m => m.Name == name && m.GetParameters().Length == argCount);

Try / catch

try
{
    var result = query.ToListAsync().GetAwaiter().GetResult();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not find a matching method"))
{
    // fall back to composing the query with supported overloads
}

Prevention

When it happens

Trigger: Calling an operator with argument types that fit no overload, e.g. wrong lambda parameter type, omitted type arguments the rewriter cannot infer, or passing a Func where an Expression is expected inside an async queryable expression.

Common situations: Migrating LINQ-to-Objects queries to async queryables where overloads differ; implicit conversions not applied inside expression trees; using int where long (or vice versa) breaks overload matching during rewrite.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableRewriter.cs:443

            //
            var targetType = type.GetTypeInfo().GetCustomAttribute<LocalQueryMethodImplementationTypeAttribute>()?.TargetType ?? type;

            //
            // Get all the candidates based on name and fail if none are found.
            //
            var methods = targetType.GetMethods(flags).Where(m => m.Name == name).ToArray();
            if (methods.Length == 0)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find method with name '{0}' on type '{1}'.", name, type));
            }

            //
            // Find a match based on arguments and fail if no match is found.
            //
            var method = methods.FirstOrDefault(m => ArgsMatch(m, args, typeArgs));
            if (method == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find a matching method with name '{0}' on type '{1}'.", name, type));
            }

            //
            // Close the generic method if needed.
            //
            if (typeArgs != null)
            {
                return method.MakeGenericMethod(typeArgs);
            }

            return method;
        }

        private static Type? FindGenericType(Type definition, Type? type)
        {
            while (type != null && type != typeof(object))
            {
                //

View on GitHub (pinned to 94b5d5ab91)