{"record":{"id":"78e3e93c3119c990","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr","errorCode":null,"errorMessage":"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.","messagePattern":"The LINQ 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\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosProjectionBindingExpressionVisitor.cs","lineNumber":120,"sourceCode":"\n            case NewExpression or MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression:\n                return base.Visit(expression);\n\n            case not null when _clientEval:\n            {\n                switch (expression)\n                {\n                    case ConstantExpression:\n                        return expression;\n\n                    case QueryParameterExpression queryParameter:\n                        return Expression.Call(\n                            GetParameterValueMethodInfo.MakeGenericMethod(queryParameter.Type),\n                            QueryCompilationContext.QueryContextParameter,\n                            Expression.Constant(queryParameter.Name));\n\n                    case ParameterExpression parameterExpression:\n                        throw new InvalidOperationException(CoreStrings.TranslationFailed(parameterExpression.Print()));\n\n                    case MaterializeCollectionNavigationExpression:\n                        return base.Visit(expression);\n                }\n\n                switch (_sqlTranslator.TranslateProjection(expression))\n                {\n                    case SqlExpression sqlExpression:\n                        return new ProjectionBindingExpression(\n                            _selectExpression, _selectExpression.AddToProjection(sqlExpression), expression.Type.MakeNullable());\n\n                    case StructuralTypeShaperExpression shaper:\n                        return base.Visit(shaper);\n                }\n\n                if (expression is MethodCallExpression\n                    {\n                        Method: { IsGenericMethod: true } method,","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosProjectionBindingExpressionVisitor.cs#L102-L138","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before - references client param x inside projection after partial server eval\nvar names = ctx.Docs\n    .Select(d => Format(d, suffix))   // suffix is a closure param\n    .AsEnumerable()\n    .Select(n => n + suffix);\n\n// after - materialize first, project on the client\nvar names = ctx.Docs.AsEnumerable()\n    .Select(d => Format(d, suffix));","handlingStrategy":"try-catch","validationCode":"// Before running, sanity-check that the projection does not reference unbound parameters by compiling it locally.\ntry { _ = compiled.Compile()(ctx); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{ /* fall back to client-side projection */ }","typeGuard":null,"tryCatchPattern":"try { var r = await ctx.Docs.Select(d => Project(d, x)).ToListAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{ var r = ctx.Docs.AsEnumerable().Select(d => Project(d, x)); }","preventionTips":["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."],"tags":["cosmos","linq","query-translation","client-eval"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}