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
Inside ShaperProcessingExpressionVisitor, GetProjectionIndex resolves a ProjectionBindingExpression to a SelectExpression projection index. If the binding has no ProjectionMember and a null Index, it cannot be remapped and the visitor throws CoreStrings.TranslationFailed with the printed expression — meaning the projection binding could not be tied to any Cosmos SQL projection.
Solutions
- Simplify the query (remove computed projections, grouping, subqueries) to isolate the binding that fails.
- Avoid custom expression-tree manipulation that creates ProjectionBindingExpression without a backing projection.
- Report the minimal repro to the EF Cosmos provider if it reproduces on stock LINQ.
Defensive patterns
Strategy: try-catch
Try / catch
try { await query.ToListAsync(ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
logger.LogWarning(ex, "Projection binding failed; simplifying query");
// simplify projection and retry
} Prevention
- Do not introduce ProjectionBindingExpression from custom visitors.
- Keep relational vs Cosmos query visitors separate.
- Add a smoke test per projection to catch binding regressions.
When it happens
Trigger: A query whose shaper references a projection binding that the Cosmos SelectExpression never produced — usually a binding created by a relational-only translation step or by a hand-built ExpressionTree.
Common situations: Mixing relational query visitors with Cosmos queries, custom IQueryTranslationPostprocessors that introduce bindings, or hitting an EF provider gap where a binding is emitted but no matching projection exists.
Related errors
- Complex projections in subqueries are currently unsupported.
- The LINQ expression ' ' could not be translated. Either…
- Unhandled expression node type
- A FromSqlExpression has an invalid arguments expression type
- Bind property on structural type coming out of scalar…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/d7dda301a6861292.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs:1478
if (_entityTypeMaterializerExpressionsMapping.TryGetValue(entityType, out var materializerExpressions))
{
return materializerExpressions;
}
}
while ((entityType = entityType.BaseType!) != null);
throw new UnreachableException();
}
private ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression)
=> ((SelectExpression)projectionBindingExpression.QueryExpression).Projection[GetProjectionIndex(projectionBindingExpression)];
private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
=> projectionBindingExpression.ProjectionMember != null
? ((SelectExpression)projectionBindingExpression.QueryExpression)
.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue<int>()
: (projectionBindingExpression.Index
?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print())));
private NewExpression NewJsonReaderManager()
=> New(
JsonReaderManagerConstructor, _jsonReaderDataParameter,
MakeMemberAccess(QueryCompilationContext.QueryContextParameter, QueryContextQueryLoggerProperty));
private static UnaryExpression ThrowInvalidToken(Expression tokenType, Type? type = null)
=> Throw(Call(CreateJsonReaderInvalidTokenTypeMethodInfo, tokenType), type ?? typeof(void));
private ParameterExpression[] GetParametersForLambda(ITypeBase structuralType)
=> structuralType.TryGetOrdinalKey(out _)
? [QueryCompilationContext.QueryContextParameter, _jsonReaderDataParameter, _ownerKeySnapshotParameter, _ordinalParameter]
: [QueryCompilationContext.QueryContextParameter, _jsonReaderDataParameter, _ownerKeySnapshotParameter];
private bool IsTracking(ITypeBase structuralType, [NotNullWhen(true)] out IEntityType? entityType)
{
entityType = structuralType as IEntityType;
return _queryStateManager && entityType != null && entityType.FindPrimaryKey() != null;View on GitHub (pinned to 3a2006ef56)