{"record":{"id":"59441af89c3534a5","repo":"dotnet/efcore","slug":"unhandled-expression-expression-of-type-expr","errorCode":null,"errorMessage":"Unhandled expression '{expression}' of type '{expressionType}' encountered in '{visitor}'.","messagePattern":"Unhandled expression '(.+?)' of type '(.+?)' encountered in '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs","lineNumber":97,"sourceCode":"            ObjectFunctionExpression e => VisitObjectFunction(e),\n            ObjectReferenceExpression e => VisitObjectReference(e),\n            OrderingExpression e => VisitOrdering(e),\n            ProjectionExpression e => VisitProjection(e),\n            ScalarAccessExpression e => VisitScalarAccess(e),\n            ScalarArrayExpression e => VisitScalarArray(e),\n            ScalarReferenceExpression e => VisitValueReference(e),\n            ScalarSubqueryExpression e => VisitScalarSubquery(e),\n            SelectExpression e => VisitSelect(e),\n            SourceExpression e => VisitSource(e),\n            SqlBinaryExpression e => VisitSqlBinary(e),\n            SqlConditionalExpression e => VisitSqlConditional(e),\n            SqlConstantExpression e => VisitSqlConstant(e),\n            SqlFunctionExpression e => VisitSqlFunction(e),\n            SqlParameterExpression e => VisitSqlParameter(e),\n            SqlUnaryExpression e => VisitSqlUnary(e),\n            StructuralTypeProjectionExpression e => VisitStructuralTypeProjection(e),\n\n            _ => throw new InvalidOperationException(\n                CosmosStrings.UnhandledExpressionInVisitor(expression, expression.GetType(), nameof(CosmosQuerySqlGenerator))),\n        };\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 virtual Expression VisitStructuralTypeProjection(StructuralTypeProjectionExpression structuralTypeProjectionExpression)\n    {\n        Visit(structuralTypeProjectionExpression.Object);\n\n        return structuralTypeProjectionExpression;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs#L79-L115","documentation":"Thrown by CosmosQuerySqlGenerator in its Expression switch default arm: a SqlExpression subclass arrived that the generator has no Visit method for. The supported set is ScalarReferenceExpression, ScalarSubqueryExpression, SelectExpression, SourceExpression, SqlBinaryExpression, SqlConditionalExpression, SqlConstantExpression, SqlFunctionExpression, SqlParameterExpression, SqlUnaryExpression, StructuralTypeProjectionExpression. Anything else indicates either a new SqlExpression type the Cosmos generator does not yet handle, or a query that the translator built into an unsupported shape.","triggerScenarios":"Writing a LINQ query whose translation produces a SqlExpression not in the generator's dispatch table (e.g. a newly supported relational expression type, or a provider extension). Usually surfaces as an internal InvalidOperationException at SQL-generation time.","commonSituations":"Upgrading EF Core where the relational layer emits a new SqlExpression kind the Cosmos generator has not been updated for; using query features (e.g. complex aggregates, window-like constructs) only relational supports; provider plugins injecting SqlExpression subclasses.","solutions":["Rewrite the query to a simpler form the Cosmos SQL dialect supports (Cosmos SQL is a subset of T-SQL).","Move the unsupported operation to the client via AsEnumerable/ToList before it reaches SQL generation.","Confirm you are on the latest EF Core Cosmos patch; if the error persists on a standard query, file an issue with the full expression tree."],"exampleFix":"// before - aggregate/construct Cosmos cannot generate SQL for\nvar r = ctx.Docs.GroupBy(d => d.Cat).Select(g => new { g.Key, Max = g.Max(d => d.Score) });\n\n// after - aggregate client-side\nvar r = ctx.Docs.AsEnumerable()\n    .GroupBy(d => d.Cat)\n    .Select(g => new { g.Key, Max = g.Max(d => d.Score) });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var r = await ctx.Docs.GroupBy(d => d.Cat).Select(g => new { g.Key, Max = g.Max(d => d.Score) }).ToListAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Unhandled expression\") || ex.Message.Contains(\"could not be translated\"))\n{ var raw = await ctx.Docs.AsNoTracking().ToListAsync();\n  var r = raw.GroupBy(d => d.Cat).Select(g => new { g.Key, Max = g.Max(d => d.Score) }); }","preventionTips":["Keep queries within the Cosmos SQL subset; aggregate/join/window operations often need client eval.","Upgrade to the latest EF Core Cosmos patch before assuming a construct is unsupported.","Reproduce the failing query in the Cosmos query explorer to confirm it cannot be expressed server-side."],"tags":["cosmos","linq","query-translation","sql-generation","internal"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}