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
Thrown by CosmosProjectionBindingExpressionVisitor during CLIENT evaluation (_clientEval == true) when the projection contains a ParameterExpression that the visitor cannot translate into a Cosmos SQL projection. ParameterExpression here is a LINQ parameter the translator did not bind to a query value, so it bails out with CoreStrings.TranslationFailed.
Solutions
- Move the projection fully server-side: only reference entity properties and translatable functions inside Select.
- If you must compute on the client, materialize first (AsEnumerable/ToList) and then project, so the parameter is evaluated locally.
- Capture local values as explicit query parameters (e.g. via a constant or a properly captured closure) rather than as LINQ parameters.
Example fix
// before - references client param x inside projection after partial server eval
var names = ctx.Docs
.Select(d => Format(d, suffix)) // suffix is a closure param
.AsEnumerable()
.Select(n => n + suffix);
// after - materialize first, project on the client
var names = ctx.Docs.AsEnumerable()
.Select(d => Format(d, suffix)); Defensive patterns
Strategy: try-catch
Validate before calling
// Before running, sanity-check that the projection does not reference unbound parameters by compiling it locally.
try { _ = compiled.Compile()(ctx); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{ /* fall back to client-side projection */ } Try / catch
try { var r = await ctx.Docs.Select(d => Project(d, x)).ToListAsync(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{ var r = ctx.Docs.AsEnumerable().Select(d => Project(d, x)); } Prevention
- Do not reference local lambda parameters inside server-side Select.
- Materialize before projecting if any client computation is needed.
- Capture values as constants rather than as LINQ parameters.
When it happens
Trigger: Projecting an expression that references a parameter the Cosmos query pipeline cannot bind, typically because client-side evaluation was forced (e.g. via AsEnumerable after a partial projection) and a closure/parameter value could not be turned into a query parameter.
Common situations: Mixing server- and client-side computation in a Select; referencing a local variable or method parameter inside a projection that the translator cannot capture; embedding a delegate or Func result in the projection.
Related errors
- Including navigation
- The LINQ expression ' ' could not be translated. Either…
- Unhandled expression
- 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/78e3e93c3119c990.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Query/Internal/CosmosProjectionBindingExpressionVisitor.cs:120
case NewExpression or MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression:
return base.Visit(expression);
case not null when _clientEval:
{
switch (expression)
{
case ConstantExpression:
return expression;
case QueryParameterExpression queryParameter:
return Expression.Call(
GetParameterValueMethodInfo.MakeGenericMethod(queryParameter.Type),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(queryParameter.Name));
case ParameterExpression parameterExpression:
throw new InvalidOperationException(CoreStrings.TranslationFailed(parameterExpression.Print()));
case MaterializeCollectionNavigationExpression:
return base.Visit(expression);
}
switch (_sqlTranslator.TranslateProjection(expression))
{
case SqlExpression sqlExpression:
return new ProjectionBindingExpression(
_selectExpression, _selectExpression.AddToProjection(sqlExpression), expression.Type.MakeNullable());
case StructuralTypeShaperExpression shaper:
return base.Visit(shaper);
}
if (expression is MethodCallExpression
{
Method: { IsGenericMethod: true } method,View on GitHub (pinned to 3a2006ef56)