{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs#L489-L525","documentation":"A generic translation-failure thrown by VisitLambda in the Cosmos SQL translating visitor. When the visitor encounters a lambda expression it cannot translate into Cosmos SQL (because no member/method translator handled its body), it throws to signal that the query as written cannot run server-side and must be rewritten or switched to client evaluation.","triggerScenarios":"Using a lambda in a query operator whose body calls unsupported methods, references client-only state, or uses C# constructs the Cosmos translator cannot map to SQL (e.g. delegates, local function calls, complex conditionals).","commonSituations":"Calling helper methods inside Where/Select lambdas, using delegates, capturing non-parameter locals that aren't translatable, or using operators unsupported by the Cosmos SQL dialect.","solutions":["Rewrite the lambda to use only Cosmos-translatable operators and property accesses.","Insert AsAsyncEnumerable()/AsQueryable() before the untranslatable operator to evaluate that part on the client.","Extract computed values into query parameters before the query if they must be passed in.","Consult the EF Core Cosmos supported-query documentation for the operator set."],"exampleFix":"// before\nvar q = context.Blogs.Where(b => Helper.IsValid(b.Title));\n// after\nvar q = await context.Blogs\n    .Select(b => b.Title)\n    .AsAsyncEnumerable()\n    .Where(t => Helper.IsValid(t))\n    .ToListAsync();","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    // pull raw data, then apply unsupported lambda client-side\n    return await query.AsAsyncEnumerable().Where(predicate).ToListAsync();\n}","preventionTips":["Restrict Where/Select lambda bodies to Cosmos-translatable constructs.","Move helper-method logic to client-side evaluation after AsAsyncEnumerable.","Maintain a query-translation test suite to catch unsupported lambdas early."],"tags":["cosmos","query","translation","linq","lambda"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}