{"record":{"id":"d302b7a23ec66bf7","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-d302b7","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.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs","lineNumber":131,"sourceCode":"\n            case null:\n                return null;\n\n            case not null when _indexBasedBinding:\n            {\n                switch (expression)\n                {\n                    case ConstantExpression:\n                        return expression;\n\n                    case QueryParameterExpression queryParameterExpression:\n                        return Expression.Call(\n                            GetParameterValueMethodInfo.MakeGenericMethod(queryParameterExpression.Type),\n                            QueryCompilationContext.QueryContextParameter,\n                            Expression.Constant(queryParameterExpression.Name));\n\n                    case ParameterExpression parameterExpression:\n                        throw new InvalidOperationException(CoreStrings.TranslationFailed(parameterExpression.Print()));\n\n                    case ProjectionBindingExpression projectionBindingExpression:\n                        return _selectExpression.GetProjection(projectionBindingExpression) switch\n                        {\n                            StructuralTypeProjectionExpression projection => AddClientProjection(projection, typeof(ValueBuffer)),\n                            SqlExpression mappedSqlExpression => AddClientProjection(mappedSqlExpression, expression.Type.MakeNullable()),\n                            // A single-result subquery (e.g. the group element of GroupBy(k).Select(g => g.First()))\n                            // being composed over: lower it into the current select as a to-one join so the result\n                            // has a bindable shape, instead of failing.\n                            ShapedQueryExpression\n                            {\n                                ResultCardinality: ResultCardinality.Single or ResultCardinality.SingleOrDefault\n                            } singleResultSubquery\n                                => LowerSingleResultSubquery(projectionBindingExpression, singleResultSubquery),\n                            _ => throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()))\n                        };\n\n                    case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs#L113-L149","documentation":"Thrown by RelationalProjectionBindingExpressionVisitor (line 131) when an index-based (client-side) projection encounters a ParameterExpression that cannot be translated to SQL. This is EF's generic 'LINQ expression could not be translated' failure: the query contains a construct that has no SQL equivalent, so EF refuses to evaluate it server-side and (without explicit client-evaluation opt-in) aborts. The message directs you to force client evaluation explicitly if that is intended.","triggerScenarios":"A Select/Where/etc. projection references a parameter or method that cannot be translated to SQL (e.g. a delegate parameter, an unmapped property, calling arbitrary .NET methods in the projection). Triggered specifically in the index-based binding fallback path where a ParameterExpression reaches translation.","commonSituations":"Projecting a closure variable into the query; calling a custom C# method in Select; referencing a non-mapped property; using .NET APIs with no SQL translation (e.g. complex string/date manipulation); upgrading EF where a previously client-evaluated construct now throws.","solutions":["Rewrite the projection to use only translatable operations (mapped properties, supported LINQ operators).","If client evaluation is intended, insert AsEnumerable()/ToListAsync() before the non-translatable part to switch to in-memory.","Move arbitrary computations out of the IQueryable and apply them after materialization.","Replace custom method calls with expression-tree-compatible equivalents or raw SQL (FromSqlRaw) where needed.","Check the linked docs (go.microsoft.com/fwlink/?linkid=2101038) for supported translation patterns."],"exampleFix":"// before - custom method + closure in projection cannot translate\nvar prefix = GetPrefix();\nvar q = context.Users.Select(u => FormatLabel(u.Name, prefix)).ToList();\n\n// after - materialize first, then compute client-side\nvar users = context.Users.Select(u => new { u.Id, u.Name }).AsEnumerable()\n    .Select(u => FormatLabel(u.Name, prefix)).ToList();","handlingStrategy":"validation","validationCode":"// Keep non-translatable logic out of the IQueryable projection.\n// Project only mapped properties server-side, then compute client-side.\nvar server = ctx.Users.Select(u => new { u.Id, u.Name }).AsEnumerable();\nvar result = server.Select(u => ClientOnly(u.Name)).ToList();","typeGuard":"// Heuristic: avoid closures/custom methods inside IQueryable Select.\nstatic bool IsTranslatable(Expression e) => e is MemberExpression or BinaryExpression\n    || e is MethodCallExpression m && (m.Method.DeclaringType == typeof(EF)\n        || m.Method.DeclaringType == typeof(Math) || m.Method.DeclaringType == typeof(string));","tryCatchPattern":"try { var q = ctx.Users.Select(u => Client(u.Name)).ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\")) {\n    // Force client evaluation by materializing first.\n    var data = ctx.Users.Select(u => new { u.Name }).AsEnumerable().Select(u => Client(u.Name)).ToList();\n}","preventionTips":["Avoid closures and custom C# methods inside IQueryable projections.","Use AsEnumerable()/ToListAsync() to move non-translatable logic client-side.","Keep projections to mapped properties and supported LINQ operators.","Review the supported-translation docs before using exotic operators."],"tags":["ef-core","linq","translation","client-eval","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}