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

A generic translation-failure thrown by VisitLambda in the Cosmos SQL translating visitor. When the visitor encounters a lambda expression it cannot translate into Cosmos SQL (because no member/method translator handled its body), it throws to signal that the query as written cannot run server-side and must be rewritten or switched to client evaluation.

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

Solutions

  1. Rewrite the lambda to use only Cosmos-translatable operators and property accesses.
  2. Insert AsAsyncEnumerable()/AsQueryable() before the untranslatable operator to evaluate that part on the client.
  3. Extract computed values into query parameters before the query if they must be passed in.
  4. Consult the EF Core Cosmos supported-query documentation for the operator set.

Example fix

// before
var q = context.Blogs.Where(b => Helper.IsValid(b.Title));
// after
var q = await context.Blogs
    .Select(b => b.Title)
    .AsAsyncEnumerable()
    .Where(t => Helper.IsValid(t))
    .ToListAsync();
Defensive patterns

Strategy: fallback

Try / catch

try
{
    return await query.ToListAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
    // pull raw data, then apply unsupported lambda client-side
    return await query.AsAsyncEnumerable().Where(predicate).ToListAsync();
}

Prevention

When it happens

Trigger: Using a lambda in a query operator whose body calls unsupported methods, references client-only state, or uses C# constructs the Cosmos translator cannot map to SQL (e.g. delegates, local function calls, complex conditionals).

Common situations: Calling helper methods inside Where/Select lambdas, using delegates, capturing non-parameter locals that aren't translatable, or using operators unsupported by the Cosmos SQL dialect.

Related errors


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