dotnet/efcore · error · InvalidOperationException

The LINQ expression '{expression}' could not be translated.

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

Thrown by the InMemory query translator when it encounters a LambdaExpression it cannot fold into a translatable expression tree. The InMemory provider only supports a subset of LINQ translation; bare lambdas that are not part of a recognized method (e.g. standalone Func passed to a custom method) hit VisitLambda and the provider surfaces CoreStrings.TranslationFailed with the offending expression.

Source

Thrown at src/EFCore.InMemory/Query/Internal/InMemoryExpressionTranslatingExpressionVisitor.cs:518

        };

    /// <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)

Solutions

  1. Rewrite the query to use only InMemory-translatable operators (Where/Select/OrderBy/GroupBy/Join and primitive comparisons).
  2. Force client evaluation deliberately by inserting .AsEnumerable() before the unsupported operator.
  3. Replace custom extension methods with their primitive equivalents (e.g. compare fields directly instead of through a wrapper).
  4. Move the lambda logic out of the queryable pipeline into a post-projection Select on already-materialized data.

Example fix

// before
db.Users.Where(u => MyLocalLambda(u))
// after
db.Users.Where(u => u.Active && u.Age > 18)
// or force client eval
db.Users.AsEnumerable().Where(u => MyLocalLambda(u))
Defensive patterns

Strategy: fallback

Try / catch

try { return db.Users.Where(predicate).ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
    return db.Users.AsEnumerable().Where(predicate.Compile()).ToList();
}

Prevention

When it happens

Trigger: A LINQ query over an InMemory DbSet whose tree contains a LambdaExpression node the translator does not handle (e.g. invoking a local lambda via a non-translatable method, or composing on an IQueryable after client-side transforms).

Common situations: Calling a helper method that takes an Expression<Func<T,bool>> the InMemory translator does not recognize. Mixing in-memory and relational LINQ idioms. Using methods with no InMemory translator (certain string, math, or custom extension methods).

Related errors


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