{"record":{"id":"f42b6a7a9fbfd798","repo":"dotnet/efcore","slug":"translationfailed","errorCode":"TranslationFailed","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/RelationalQueryableMethodTranslatingExpressionVisitor.cs","lineNumber":140,"sourceCode":"                    {\n                        string call;\n                        var methodInfo = function.DbFunctions.Last().MethodInfo;\n                        if (methodInfo != null)\n                        {\n                            var methodCall = Expression.Call(\n                                // Declaring types would be derived db context.\n                                Expression.Default(methodInfo.DeclaringType!),\n                                methodInfo,\n                                tableValuedFunctionQueryRootExpression.Arguments);\n\n                            call = methodCall.Print();\n                        }\n                        else\n                        {\n                            call = $\"{function.DbFunctions.Last().Name}()\";\n                        }\n\n                        throw new InvalidOperationException(\n                            TranslationErrorDetails == null\n                                ? CoreStrings.TranslationFailed(call)\n                                : CoreStrings.TranslationFailedWithDetails(call, TranslationErrorDetails));\n                    }\n\n                    arguments.Add(sqlArgument);\n                }\n\n                var entityType = tableValuedFunctionQueryRootExpression.EntityType;\n                var alias = _sqlAliasManager.GenerateTableAlias(function);\n                var translation = new TableValuedFunctionExpression(alias, function, arguments);\n                var queryExpression = CreateSelect(entityType, translation);\n\n                return CreateShapedQueryExpression(entityType, queryExpression);\n            }\n\n            case EntityQueryRootExpression entityQueryRootExpression\n                when entityQueryRootExpression.GetType() == typeof(EntityQueryRootExpression)","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs#L122-L158","documentation":"The generic translation-failure error. In the TableValuedFunctionQueryRootExpression case (line 114-155) and broadly across query translation, when an expression cannot be translated to SQL and no accumulated error details exist, CoreStrings.TranslationFailed is thrown. It reports the offending expression and points to client-evaluation alternatives (AsEnumerable/ToListAsync).","triggerScenarios":"A LINQ query (including TVF argument translation) containing a construct the provider cannot turn into SQL: a client-side method, an unsupported operator, a C# feature with no SQL equivalent, or an unmapped member — and no detailed sub-errors were collected.","commonSituations":"Calling custom methods or static helpers in Where/Select; using unsupported date/string operations; referencing properties not in the model; provider limitations (e.g. certain functions on SQLite); LINQ that worked on one provider failing on another.","solutions":["Rewrite the query to use only provider-translatable constructs; replace client-side methods with captured parameters or EF.Functions equivalents.","If the operation genuinely must run client-side, insert AsEnumerable()/ToListAsync() before the unsupported operator to force client evaluation.","Map server-side functions via HasDbFunction for reusable SQL functions.","Simplify the query and add pieces back incrementally to isolate the untranslatable fragment."],"exampleFix":"// before (client-side method in Where)\nvar active = db.Users.Where(u => Helper.IsActive(u.Status)).ToList();\n\n// after (capture the value as a parameter, or translate the predicate)\nvar activeStatus = Helper.ActiveStatusValue;\nvar active = db.Users.Where(u => u.Status == activeStatus).ToList();\n// or force client evaluation explicitly:\nvar active = db.Users.AsEnumerable().Where(u => Helper.IsActive(u.Status)).ToList();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var r = await query.ToListAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{ /* isolate the untranslatable fragment; rewrite or move client-side with AsEnumerable */ }","preventionTips":["Keep Where/Select lambdas free of client-side methods; capture values as parameters.","Use EF.Functions and HasDbFunction for server-side operations.","Build queries incrementally and test translation early to catch failures near their source.","Avoid unmapped members and unsupported C# constructs in translatable expressions."],"tags":["efcore","query-translation","linq","client-evaluation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}