dotnet/efcore · error · InvalidOperationException

The LINQ expression ' ' could not be translated. Either…

Error message

The LINQ expression '{expression}' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

What it means

VisitLambda in CosmosSqlTranslatingExpressionVisitor throws when an unreduced LambdaExpression reaches the Cosmos SQL translation phase. Cosmos SQL has no concept of inline lambda expressions, so any lambda that has not been reduced earlier in the query pipeline is untranslatable. The exception carries the standard EF Core translation-failure message directing the user to client evaluation.

Solutions

  1. Rewrite the predicate in terms of operators EF can translate (Where/Select/OrderBy over mapped members).
  2. Insert AsEnumerable() or ToListAsync() before the unsupported operation to evaluate it client-side.
  3. If the method is a custom extension, register a method translator (IMethodCallTranslator) or replace with a recognized construct.
  4. Check the EF Core release notes / breaking changes for removed translators after an upgrade.

Example fix

// before
var q = db.Users.Where(u => MyHelper.IsMatch(u.Name));

// after - inline the predicate so EF can translate it
var q = db.Users.Where(u => u.Name.StartsWith("x"));
Defensive patterns

Strategy: fallback

Validate before calling

// Audit query expressions for unsupported method calls before execution.
var unsupported = new[] { "MyHelper.IsMatch", "SomeCustomExt" };
bool IsSafe(Expression e) => !new ExpressionFinder(unsupported).Any(e);

Try / catch

try { return await query.ToListAsync(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{ return (await query.AsAsyncEnumerable().ToListAsync()).Where(predicate).ToList(); }

Prevention

When it happens

Trigger: Any LINQ pattern that yields a surviving LambdaExpression at translation time: invoking a custom static/instance method EF has no translator for, Delegate.Invoke, lambdas passed to unmapped extension methods, or predicates built via reflection/Expression.Call to unsupported methods.

Common situations: Calling helper/static methods inside Where/Select; using unmapped extension methods; mixing in C# local functions inside queries; version upgrades that remove a previously-supported translator.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/6557318089c92791. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs:507

    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override Expression VisitInvocation(InvocationExpression invocationExpression)
        => QueryCompilationContext.NotTranslatedExpression;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override Expression VisitLambda<T>(Expression<T> lambdaExpression)
        => throw new InvalidOperationException(CoreStrings.TranslationFailed(lambdaExpression.Print()));

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override Expression VisitListInit(ListInitExpression listInitExpression)
        => QueryCompilationContext.NotTranslatedExpression;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected override Expression VisitMember(MemberExpression memberExpression)
    {

View on GitHub (pinned to 3a2006ef56)