{"record":{"id":"51a720842464ae3f","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-51a720","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/CosmosQueryTranslationPostprocessor.cs","lineNumber":173,"sourceCode":"            {\n                return;\n            }\n\n            foreach (var primaryKeyProperty in primaryKeyProperties)\n            {\n                if (string.Equals(primaryKeyProperty.GetJsonPropertyName(), propertyName, StringComparison.Ordinal))\n                {\n                    ProjectedKeyProperties.Add(primaryKeyProperty);\n                    return;\n                }\n            }\n        }\n\n        private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)\n            => projectionBindingExpression.ProjectionMember is not null\n                ? selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue<int>()\n                : projectionBindingExpression.Index\n                ?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));\n\n        private bool IsRootEntityType(IEntityType entityType)\n            => entityType == rootEntityType\n                || entityType.GetRootType() == rootEntityType.GetRootType();\n    }\n\n    private sealed class ProjectionBindingIndexRemappingExpressionVisitor(\n        SelectExpression selectExpression,\n        IReadOnlyList<int> oldIndexToNewIndex)\n        : ExpressionVisitor\n    {\n        protected override Expression VisitExtension(Expression node)\n            => node switch\n            {\n                ShapedQueryExpression => node,\n                ProjectionBindingExpression\n                {\n                    QueryExpression: var queryExpression,","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs#L155-L191","documentation":"A generic translation-failure error thrown during Cosmos query postprocessing when a ProjectionBindingExpression cannot be resolved to a concrete projection index (both ProjectionMember and Index are null). It indicates the LINQ expression tree could not be mapped onto the Cosmos SQL projection model, so the query cannot execute server-side.","triggerScenarios":"Using LINQ operators or constructs in the Select/projection that the Cosmos provider cannot translate to Cosmos SQL, leaving an unresolvable projection binding. Often triggered by client-side method calls, unsupported aggregations, or projection shapes the Cosmos SelectExpression cannot represent.","commonSituations":"Projecting computed properties that call non-translatable methods, using custom extension methods in Select, or relying on C# constructs (e.g. complex constructors, dynamic dispatch) the Cosmos translator does not understand.","solutions":["Rewrite the query to project only translatable properties and supported expressions.","Insert AsAsyncEnumerable() before the untranslatable projection to force client evaluation of that part.","Simplify the Select to entity properties first, then project the DTO on the client.","Check the EF Core Cosmos provider version supports the operator you are using."],"exampleFix":"// before\nvar result = await context.Blogs\n    .Select(b => new BlogDto { Title = CustomFormatter.Format(b.Title) })\n    .ToListAsync();\n// after\nvar result = await context.Blogs\n    .Select(b => new { b.Title })\n    .AsAsyncEnumerable()\n    .Select(x => new BlogDto { Title = CustomFormatter.Format(x.Title) })\n    .ToListAsync();","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Wrap query execution to offer client-eval fallback\ntry\n{\n    return await query.ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{\n    return await query.AsAsyncEnumerable().ToListAsync();\n}","preventionTips":["Keep projections to translatable property accesses; move DTO mapping client-side.","Add a query-translation test suite to catch unsupported projections early.","Review EF Core Cosmos supported-query docs when adding new operators."],"tags":["cosmos","query","translation","linq","projection"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}