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

During NoTrackingWithIdentityResolution postprocessing the visitor resolves each ProjectionBindingExpression to a projection index. If a binding has neither a ProjectionMember nor an Index it cannot be remapped and EF throws the shared CoreStrings.TranslationFailed, telling you the expression printed form cannot be translated by the Cosmos provider.

Solutions

  1. Rewrite the projection to use only translatable operators (remove client-side method calls, computed properties, unsupported aggregates).
  2. Move untranslatable computation after .AsEnumerable()/.ToListAsync() so it runs client-side explicitly.
  3. Simplify the projection to confirm which element fails to translate, then re-introduce pieces one at a time.

Example fix

// before
var q = db.Users.AsNoTrackingWithIdentityResolution()
    .Select(u => new { u.Id, Label = FormatLabel(u.Name) });
// after
var raw = await db.Users.AsNoTrackingWithIdentityResolution()
    .Select(u => new { u.Id, u.Name }).ToListAsync();
var result = raw.Select(r => new { r.Id, Label = FormatLabel(r.Name) });
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    var result = await query.ToListAsync(cancellationToken);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
    logger.LogWarning(ex, "Cosmos translation failed; falling back to client eval");
    var raw = await query.AsNoTracking().Select(x => x).ToListAsync(cancellationToken);
    // project client-side
}

Prevention

When it happens

Trigger: An expression tree in the projection contains a node the Cosmos query pipeline cannot map to a SQL projection (e.g. a client-side method call, complex grouping, or unsupported aggregate) inside a NoTrackingWithIdentityResolution query that touches owned entities.

Common situations: Porting a relational query that uses client-evaluable methods in the selector, mixing owned-entity projection with custom compute, or hitting a provider gap on a newer LINQ operator.

Related errors


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

Appendix: source

Thrown at src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs:173

            {
                return;
            }

            foreach (var primaryKeyProperty in primaryKeyProperties)
            {
                if (string.Equals(primaryKeyProperty.GetJsonPropertyName(), propertyName, StringComparison.Ordinal))
                {
                    ProjectedKeyProperties.Add(primaryKeyProperty);
                    return;
                }
            }
        }

        private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
            => projectionBindingExpression.ProjectionMember is not null
                ? selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue<int>()
                : projectionBindingExpression.Index
                ?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));

        private bool IsRootEntityType(IEntityType entityType)
            => entityType == rootEntityType
                || entityType.GetRootType() == rootEntityType.GetRootType();
    }

    private sealed class ProjectionBindingIndexRemappingExpressionVisitor(
        SelectExpression selectExpression,
        IReadOnlyList<int> oldIndexToNewIndex)
        : ExpressionVisitor
    {
        protected override Expression VisitExtension(Expression node)
            => node switch
            {
                ShapedQueryExpression => node,
                ProjectionBindingExpression
                {
                    QueryExpression: var queryExpression,

View on GitHub (pinned to 3a2006ef56)