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 error thrown during Cosmos query postprocessing when a ProjectionBindingExpression cannot be resolved to a concrete projection index (both ProjectionMember and Index are null). It indicates the LINQ expression tree could not be mapped onto the Cosmos SQL projection model, so the query cannot execute server-side.
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 dbf9771522)
Solutions
- Rewrite the query to project only translatable properties and supported expressions.
- Insert AsAsyncEnumerable() before the untranslatable projection to force client evaluation of that part.
- Simplify the Select to entity properties first, then project the DTO on the client.
- Check the EF Core Cosmos provider version supports the operator you are using.
Example fix
// before
var result = await context.Blogs
.Select(b => new BlogDto { Title = CustomFormatter.Format(b.Title) })
.ToListAsync();
// after
var result = await context.Blogs
.Select(b => new { b.Title })
.AsAsyncEnumerable()
.Select(x => new BlogDto { Title = CustomFormatter.Format(x.Title) })
.ToListAsync(); Defensive patterns
Strategy: fallback
Try / catch
// Wrap query execution to offer client-eval fallback
try
{
return await query.ToListAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
return await query.AsAsyncEnumerable().ToListAsync();
} Prevention
- Keep projections to translatable property accesses; move DTO mapping client-side.
- Add a query-translation test suite to catch unsupported projections early.
- Review EF Core Cosmos supported-query docs when adding new operators.
When it happens
Trigger: Using LINQ operators or constructs in the Select/projection that the Cosmos provider cannot translate to Cosmos SQL, leaving an unresolvable projection binding. Often triggered by client-side method calls, unsupported aggregations, or projection shapes the Cosmos SelectExpression cannot represent.
Common situations: Projecting computed properties that call non-translatable methods, using custom extension methods in Select, or relying on C# constructs (e.g. complex constructors, dynamic dispatch) the Cosmos translator does not understand.
Related errors
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The '{methodName}' method is not supported because the query
- A FromSqlExpression has an invalid arguments expression type
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/51a720842464ae3f.
Report an issue: GitHub.