{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosProjectionBindingExpressionVisitor.cs#L102-L138","documentation":"Thrown by CosmosProjectionBindingExpressionVisitor during client-evaluation when it encounters a ParameterExpression that the SQL translator cannot turn into a Cosmos SQL expression. ParameterExpression here means an unbound lambda parameter that the provider could not push down or rewrite; the inner SQL translator already failed and the visitor cannot recover, so it surfaces the generic TranslationFailed message with the offending expression printed.","triggerScenarios":"A LINQ query whose projection references a captured/parameter variable in a way Cosmos cannot translate, e.g. projecting ctx.Set<T>().Where(x => x.Id == someLocalVar) and then composing a non-translatable selector over it, forcing partial client eval that hits a raw parameter.","commonSituations":"Using unsupported LINQ operators in the Select clause, or referencing external queryables/parameters inside the projection that the Cosmos translator does not understand. Combining ToArray on a subquery composed with operators the provider cannot push down.","solutions":["Simplify the projection to only translatable operators; move the non-translatable logic after a client-side boundary such as AsEnumerable().","Replace parameter references with constants where the value is fixed, or pass them via FromSqlRaw parameters if supported.","Restructure the query so the part referencing the parameter is evaluated client-side after AsEnumerable, not in the server projection."],"exampleFix":"// before\nvar prefix = GetPrefix();\nvar results = await db.Items\n    .Select(i => Format(i.Name, prefix)) // Format is not translatable\n    .ToListAsync();\n\n// after\nvar results = (await db.Items.Select(i => i.Name).ToListAsync())\n    .Select(name => Format(name, prefix));","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    return await query.ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{\n    // Fall back: materialize server-side, finish shaping client-side\n    return (await query.AsQueryable().Select(x => /* only scalars */).ToListAsync());\n}","preventionTips":["Keep server-side projections to supported operators only; move formatting and custom logic after AsEnumerable.","Prefer passing parameters via the query pipeline (FromSqlRaw) rather than captured variables inside non-translatable selectors."],"tags":["cosmos","ef-core","query","translation","linq"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}