{"record":{"id":"fa153238c97c8d55","repo":"dotnet/efcore","slug":"encountered-syntaxkind-isexpression-with-non-con","errorCode":null,"errorMessage":"Encountered {SyntaxKind.IsExpression} 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":315,"sourceCode":"            SyntaxKind.ExclusiveOrExpression when left.Type.IsEnum || right.Type.IsEnum\n                => Convert(\n                    ExclusiveOr(Convert(left, left.Type.GetEnumUnderlyingType()), Convert(right, right.Type.GetEnumUnderlyingType())),\n                    left.Type),\n\n            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>","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L297-L333","documentation":"In the `SyntaxKind.IsExpression` arm of `VisitBinaryExpression`, the translator requires the right operand to be a `ConstantExpression` whose `Value` is a `Type` (i.e. `x is SomeType`). If the right side is not a constant type it throws `InvalidOperationException`, because richer pattern-matching forms (`is int y`, `is { Length: > 0 }`) cannot be represented.","triggerScenarios":"A precompiled query using C# pattern matching on the left of `is` with anything other than a bare type, e.g. `obj is int n`, `o is string { Length: > 0 }`, or a switch-expression-style `is` pattern.","commonSituations":"Writing modern C# patterns inside a query lambda; the compiler accepts them but the translator's expression-tree model does not.","solutions":["Use a plain type test (`x is SomeType`) and follow with explicit casts/member access.","Move the pattern-matching logic out of the query and apply it to materialized results."],"exampleFix":"// before\nvar q = ctx.Items.Where(i => (i.Data as object) is int n && n > 0);\n// after\nvar q = ctx.Items.Where(i => i.Data is int).Select(i => (int)i.Data).Where(n => n > 0);","handlingStrategy":"validation","validationCode":"// Scan for non-trivial 'is' patterns the translator cannot handle.\nusing Microsoft.CodeAnalysis.CSharp.Syntax;\nusing Microsoft.CodeAnalysis.CSharp;\nbool unsupportedIs = node.DescendantNodes().OfType<BinaryExpressionSyntax>()\n    .Any(b => b.IsKind(SyntaxKind.IsExpression)\n              && !(b.Right is PredefinedTypeSyntax || b.Right is IdentifierNameSyntax || b.Right is QualifiedNameSyntax));\nif (unsupportedIs)\n    throw new NotSupportedException(\"Only plain type tests (`x is T`) 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(\"IsExpression\") && ex.Message.Contains(\"non-constant type\"))\n{ /* rewrite the pattern as a plain type test + casts */ }","preventionTips":["Use plain type tests (`x is T`) inside queries; avoid `is T v` and recursive patterns there.","Apply complex pattern matching after materialization."],"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"}