{"record":{"id":"d2823f74cb60ed96","repo":"dotnet/efcore","slug":"encountered-syntaxkind-asexpression-with-non-con","errorCode":null,"errorMessage":"Encountered {SyntaxKind.AsExpression} with non-constant type right argument: {right}","messagePattern":"Encountered (.+?) with non-constant type right argument: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":320,"sourceCode":"            SyntaxKind.BitwiseOrExpression => Or(left, right),\n            SyntaxKind.BitwiseAndExpression => And(left, right),\n            SyntaxKind.ExclusiveOrExpression => ExclusiveOr(left, right),\n\n            SyntaxKind.EqualsExpression => Equal(left, right),\n            SyntaxKind.NotEqualsExpression => NotEqual(left, right),\n            SyntaxKind.LessThanExpression => LessThan(left, right),\n            SyntaxKind.LessThanOrEqualExpression => LessThanOrEqual(left, right),\n            SyntaxKind.GreaterThanExpression => GreaterThan(left, right),\n            SyntaxKind.GreaterThanOrEqualExpression => GreaterThanOrEqual(left, right),\n            SyntaxKind.IsExpression => TypeIs(\n                left, right is ConstantExpression { Value: Type type }\n                    ? type\n                    : throw new InvalidOperationException(\n                        $\"Encountered {SyntaxKind.IsExpression} with non-constant type right argument: {right}\")),\n            SyntaxKind.AsExpression => TypeAs(\n                left, right is ConstantExpression { Value: Type type }\n                    ? type\n                    : throw new InvalidOperationException(\n                        $\"Encountered {SyntaxKind.AsExpression} with non-constant type right argument: {right}\")),\n            SyntaxKind.CoalesceExpression => Coalesce(left, right),\n\n            _ => throw new ArgumentOutOfRangeException($\"BinaryExpressionSyntax with {binary.Kind()}\")\n        };\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    public override Expression VisitCastExpression(CastExpressionSyntax cast)\n        => Convert(Visit(cast.Expression), ResolveType(cast.Type));\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L302-L338","documentation":"In the `SyntaxKind.AsExpression` arm of `VisitBinaryExpression`, the translator needs the right operand to be a `ConstantExpression` holding a `Type` (plain `x as SomeType`). Any non-constant right side throws `InvalidOperationException`; only the simple cast-or-null form is translatable.","triggerScenarios":"A precompiled query using `as` with something other than a bare type reference, or where the translator cannot fold the right side to a constant `Type`.","commonSituations":"Complex generics or pattern-based `as` usage inside a query; usually the compiler keeps `as Type` simple, so this is rare in hand-written code but possible with generated/synthesized trees.","solutions":["Keep the `as` expression as a plain type conversion (`x as TargetType`).","If the right side is dynamic, hoist it out of the query."],"exampleFix":"// before\nvar q = ctx.Items.Where(i => (i.Payload as varType) != null);\n// after\nvar q = ctx.Items.Where(i => i.Payload is TargetType).Select(i => (TargetType)i.Payload);","handlingStrategy":"validation","validationCode":"// Reject anything other than a plain `as Type` before translation.\nusing Microsoft.CodeAnalysis.CSharp.Syntax;\nusing Microsoft.CodeAnalysis.CSharp;\nbool unsupportedAs = node.DescendantNodes().OfType<BinaryExpressionSyntax>()\n    .Any(b => b.IsKind(SyntaxKind.AsExpression)\n              && !(b.Right is PredefinedTypeSyntax || b.Right is IdentifierNameSyntax || b.Right is QualifiedNameSyntax));\nif (unsupportedAs)\n    throw new NotSupportedException(\"Only plain `x as T` conversions are supported in precompiled queries.\");\n\ntranslator.Translate(node, semanticModel);","typeGuard":null,"tryCatchPattern":"try { translator.Translate(node, semanticModel); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AsExpression\") && ex.Message.Contains(\"non-constant type\"))\n{ /* rewrite as a plain type conversion */ }","preventionTips":["Keep `as` expressions limited to `x as ConcreteType` inside queries.","Avoid synthesized trees that put non-type nodes on the right of `as`."],"tags":["ef-core","precompiled-queries","expression-trees","pattern-matching","unsupported-construct"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}