{"record":{"id":"6557318089c92791","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-655731","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/CosmosSqlTranslatingExpressionVisitor.cs","lineNumber":507,"sourceCode":"    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected override Expression VisitInvocation(InvocationExpression invocationExpression)\n        => QueryCompilationContext.NotTranslatedExpression;\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected override Expression VisitLambda<T>(Expression<T> lambdaExpression)\n        => throw new InvalidOperationException(CoreStrings.TranslationFailed(lambdaExpression.Print()));\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected override Expression VisitListInit(ListInitExpression listInitExpression)\n        => QueryCompilationContext.NotTranslatedExpression;\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected override Expression VisitMember(MemberExpression memberExpression)\n    {","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs#L489-L525","documentation":"VisitLambda in CosmosSqlTranslatingExpressionVisitor throws when an unreduced LambdaExpression reaches the Cosmos SQL translation phase. Cosmos SQL has no concept of inline lambda expressions, so any lambda that has not been reduced earlier in the query pipeline is untranslatable. The exception carries the standard EF Core translation-failure message directing the user to client evaluation.","triggerScenarios":"Any LINQ pattern that yields a surviving LambdaExpression at translation time: invoking a custom static/instance method EF has no translator for, Delegate.Invoke, lambdas passed to unmapped extension methods, or predicates built via reflection/Expression.Call to unsupported methods.","commonSituations":"Calling helper/static methods inside Where/Select; using unmapped extension methods; mixing in C# local functions inside queries; version upgrades that remove a previously-supported translator.","solutions":["Rewrite the predicate in terms of operators EF can translate (Where/Select/OrderBy over mapped members).","Insert AsEnumerable() or ToListAsync() before the unsupported operation to evaluate it client-side.","If the method is a custom extension, register a method translator (IMethodCallTranslator) or replace with a recognized construct.","Check the EF Core release notes / breaking changes for removed translators after an upgrade."],"exampleFix":"// before\nvar q = db.Users.Where(u => MyHelper.IsMatch(u.Name));\n\n// after - inline the predicate so EF can translate it\nvar q = db.Users.Where(u => u.Name.StartsWith(\"x\"));","handlingStrategy":"fallback","validationCode":"// Audit query expressions for unsupported method calls before execution.\nvar unsupported = new[] { \"MyHelper.IsMatch\", \"SomeCustomExt\" };\nbool IsSafe(Expression e) => !new ExpressionFinder(unsupported).Any(e);","typeGuard":null,"tryCatchPattern":"try { return await query.ToListAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{ return (await query.AsAsyncEnumerable().ToListAsync()).Where(predicate).ToList(); }","preventionTips":["Avoid calling custom/static methods inside queries; inline predicates using mapped members and operators.","Keep a list of EF-translatable patterns and stick to them for server-side queries.","After EF Core upgrades, run query regression tests to detect removed translators."],"tags":["cosmos","query-translation","lambda","client-eval"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}