{"record":{"id":"d7dda301a6861292","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-d7dda3","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/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs","lineNumber":1478,"sourceCode":"                if (_entityTypeMaterializerExpressionsMapping.TryGetValue(entityType, out var materializerExpressions))\n                {\n                    return materializerExpressions;\n                }\n            }\n            while ((entityType = entityType.BaseType!) != null);\n\n            throw new UnreachableException();\n        }\n\n        private ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression)\n            => ((SelectExpression)projectionBindingExpression.QueryExpression).Projection[GetProjectionIndex(projectionBindingExpression)];\n\n        private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)\n            => projectionBindingExpression.ProjectionMember != null\n                ? ((SelectExpression)projectionBindingExpression.QueryExpression)\n                .GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue<int>()\n                : (projectionBindingExpression.Index\n                    ?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print())));\n\n        private NewExpression NewJsonReaderManager()\n            => New(\n                JsonReaderManagerConstructor, _jsonReaderDataParameter,\n                MakeMemberAccess(QueryCompilationContext.QueryContextParameter, QueryContextQueryLoggerProperty));\n\n        private static UnaryExpression ThrowInvalidToken(Expression tokenType, Type? type = null)\n            => Throw(Call(CreateJsonReaderInvalidTokenTypeMethodInfo, tokenType), type ?? typeof(void));\n\n        private ParameterExpression[] GetParametersForLambda(ITypeBase structuralType)\n            => structuralType.TryGetOrdinalKey(out _)\n                ? [QueryCompilationContext.QueryContextParameter, _jsonReaderDataParameter, _ownerKeySnapshotParameter, _ordinalParameter]\n                : [QueryCompilationContext.QueryContextParameter, _jsonReaderDataParameter, _ownerKeySnapshotParameter];\n\n        private bool IsTracking(ITypeBase structuralType, [NotNullWhen(true)] out IEntityType? entityType)\n        {\n            entityType = structuralType as IEntityType;\n            return _queryStateManager && entityType != null && entityType.FindPrimaryKey() != null;","sourceCodeStart":1460,"sourceCodeEnd":1496,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs#L1460-L1496","documentation":"A generic translation-failure thrown inside the shaper processing visitor when resolving a ProjectionBindingExpression that has neither a ProjectionMember nor an Index. This means the query's projection could not be mapped to a concrete server-side projection slot, so the compiled shaper cannot read the value.","triggerScenarios":"A LINQ projection that the Cosmos shaper compiler cannot bind to the SelectExpression's projection list, often due to unsupported projection shapes, complex DTO constructors, or client-side methods inside Select.","commonSituations":"Projecting into DTOs with non-translatable constructors or helper calls, grouping, or using operators the Cosmos provider does not support inside the projection.","solutions":["Simplify the projection to direct property bindings supported by Cosmos.","Move untranslatable projection logic to the client via AsAsyncEnumerable().","Project an anonymous type of raw properties first, then map to the DTO on the client.","Check EF Core Cosmos release notes for supported projection operators."],"exampleFix":"// before\nvar result = await context.Blogs\n    .Select(b => new BlogDto(b.Title, ComputeRank(b.Score)))\n    .ToListAsync();\n// after\nvar result = await context.Blogs\n    .Select(b => new { b.Title, b.Score })\n    .AsAsyncEnumerable()\n    .Select(x => new BlogDto(x.Title, ComputeRank(x.Score)))\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    return await query.Select(x => x /* properties only */).AsAsyncEnumerable().ToListAsync();\n}","preventionTips":["Keep projections to supported property accesses; map DTOs on the client.","Add translation tests for non-trivial projections.","Split server-side projection from client-side mapping."],"tags":["cosmos","query","translation","shaper","projection","linq"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}