{"record":{"id":"48a387676a65bb85","repo":"dotnet/efcore","slug":"complex-projections-in-subqueries-are-currently-un","errorCode":null,"errorMessage":"Complex projections in subqueries are currently unsupported.","messagePattern":"Complex projections in subqueries are currently unsupported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs","lineNumber":599,"sourceCode":"                {\n                    SqlExpression e => new ScalarReferenceExpression(joinSource.Alias, e.Type, e.TypeMapping),\n                    StructuralTypeProjectionExpression e => e.Update(new ObjectReferenceExpression(e.StructuralType, joinSource.Alias)),\n\n                    _ => throw new UnreachableException(\n                        $\"Unexpected expression type in projection when adding join: {expression.GetType().Name}\")\n                };\n            }\n            else\n            {\n                // TODO: #34004\n                // The subquery is projecting out a JSON object; for the projection mapping of the outer query, we need to generate\n                // property accesses over that object: Scalar/ObjectAccessExpressions over the ObjectReferenceExpression that references\n                // the JOIN source.\n                // However, the JSON object being projected out of the subquery doesn't correspond to any entity type, and there's currently\n                // no way for us to represent a reference to that - ObjectReferenceExpression requires an IEntityType. Changing that\n                // requires shaper-side changes (see comment in ObjectReferenceExpression); if we can remove that requirement, we can\n                // possibly also merge ScalarReferenceExpression and ObjectReferenceExpression to a single SourceReferenceExpression.\n                throw new InvalidOperationException(CosmosStrings.ComplexProjectionInSubqueryNotSupported);\n            }\n\n            projectionMapping[remappedProjectionMember] = projectionToAdd;\n        }\n\n        innerSelect.ApplyProjection();\n        _sources.Add(joinSource);\n\n        innerShaper = new ProjectionMemberRemappingExpressionVisitor(this, mapping).Visit(innerShaper);\n        _projectionMapping = projectionMapping;\n        innerSelect._projectionMapping.Clear();\n\n        return New(\n            transparentIdentifierType.GetTypeInfo().DeclaredConstructors.Single(),\n            [outerShaper, innerShaper], outerMemberInfo, innerMemberInfo);\n    }\n\n    /// <summary>","sourceCodeStart":581,"sourceCodeEnd":617,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L581-L617","documentation":"SelectExpression throws ComplexProjectionInSubqueryNotSupported when folding a subquery whose projection is a JSON object (a non-entity structural projection) into an outer query. This path is unimplemented (tracked as #34004): there is currently no way to represent a reference to a JSON object projected out of a subquery that does not correspond to an entity type. It is a provider limitation, not a model error.","triggerScenarios":"A subquery that projects an anonymous/complex object (Select new { ... }) feeding an outer query that references members of that object.","commonSituations":"Composing Select over a subquery that returns a shaped object; nested DTO projections where the outer projection drills into the inner DTO's fields.","solutions":["Flatten the subquery by projecting primitive members directly.","Force client evaluation between the subquery and the outer projection using AsEnumerable().","Project an entity type out of the subquery so ObjectReferenceExpression can be used."],"exampleFix":"// before\nvar q = db.Orders\n    .Select(o => new { Cust = new { o.Customer.Name, o.Customer.City } })\n    .Select(x => x.Cust.Name);\n\n// after - project the scalar directly\nvar q = db.Orders.Select(o => o.Customer.Name);","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return await query.ToListAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Complex projections in subqueries\"))\n{ return query.AsEnumerable().ToList(); }","preventionTips":["Project scalar members from subqueries instead of anonymous/complex objects.","Split two-step projections across an AsEnumerable boundary when the inner step shapes an object.","Track dotnet/efcore issue #34004 for resolution status."],"tags":["cosmos","query-translation","subquery","projection"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}