dotnet/efcore · error · InvalidOperationException
Complex projections in subqueries are currently unsupported.
Error message
Complex projections in subqueries are currently unsupported.
What it means
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.
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.
Example fix
// before
var q = db.Orders
.Select(o => new { Cust = new { o.Customer.Name, o.Customer.City } })
.Select(x => x.Cust.Name);
// after - project the scalar directly
var q = db.Orders.Select(o => o.Customer.Name); Defensive patterns
Strategy: fallback
Try / catch
try { return await query.ToListAsync(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Complex projections in subqueries"))
{ return query.AsEnumerable().ToList(); } Prevention
- 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.
When it happens
Trigger: A subquery that projects an anonymous/complex object (Select new { ... }) feeding an outer query that references members of that object.
Common situations: Composing Select over a subquery that returns a shaped object; nested DTO projections where the outer projection drills into the inner DTO's fields.
Related errors
- Bind property on structural type coming out of scalar…
- Subquery with index projection binding
- The LINQ expression ' ' could not be translated. Either…
- The LINQ expression ' ' could not be translated. Either…
- A FromSqlExpression has an invalid arguments expression type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/48a387676a65bb85.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs:599
{
SqlExpression e => new ScalarReferenceExpression(joinSource.Alias, e.Type, e.TypeMapping),
StructuralTypeProjectionExpression e => e.Update(new ObjectReferenceExpression(e.StructuralType, joinSource.Alias)),
_ => throw new UnreachableException(
$"Unexpected expression type in projection when adding join: {expression.GetType().Name}")
};
}
else
{
// TODO: #34004
// The subquery is projecting out a JSON object; for the projection mapping of the outer query, we need to generate
// property accesses over that object: Scalar/ObjectAccessExpressions over the ObjectReferenceExpression that references
// the JOIN source.
// However, the JSON object being projected out of the subquery doesn't correspond to any entity type, and there's currently
// no way for us to represent a reference to that - ObjectReferenceExpression requires an IEntityType. Changing that
// requires shaper-side changes (see comment in ObjectReferenceExpression); if we can remove that requirement, we can
// possibly also merge ScalarReferenceExpression and ObjectReferenceExpression to a single SourceReferenceExpression.
throw new InvalidOperationException(CosmosStrings.ComplexProjectionInSubqueryNotSupported);
}
projectionMapping[remappedProjectionMember] = projectionToAdd;
}
innerSelect.ApplyProjection();
_sources.Add(joinSource);
innerShaper = new ProjectionMemberRemappingExpressionVisitor(this, mapping).Visit(innerShaper);
_projectionMapping = projectionMapping;
innerSelect._projectionMapping.Clear();
return New(
transparentIdentifierType.GetTypeInfo().DeclaredConstructors.Single(),
[outerShaper, innerShaper], outerMemberInfo, innerMemberInfo);
}
/// <summary>View on GitHub (pinned to 3a2006ef56)