{"record":{"id":"8171278b9ebd1cf9","repo":"dotnet/efcore","slug":"power-over-non-double-operands","errorCode":null,"errorMessage":"Power over non-double operands","messagePattern":"Power over non-double operands","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":337,"sourceCode":"            case ExpressionType.OrAssign:\n                return VisitAssignment(binary, SyntaxKind.OrAssignmentExpression);\n            case ExpressionType.LeftShiftAssign:\n                return VisitAssignment(binary, SyntaxKind.LeftShiftAssignmentExpression);\n            case ExpressionType.RightShiftAssign:\n                return VisitAssignment(binary, SyntaxKind.RightShiftAssignmentExpression);\n            case ExpressionType.ExclusiveOrAssign:\n                return VisitAssignment(binary, SyntaxKind.ExclusiveOrAssignmentExpression);\n\n            case ExpressionType.Power when binary.Left.Type == typeof(double) && binary.Right.Type == typeof(double):\n                return Visit(\n                    Expression.Call(\n                        _mathPowMethod ??= typeof(Math).GetMethod(\n                            nameof(Math.Pow), BindingFlags.Static | BindingFlags.Public, [typeof(double), typeof(double)])!,\n                        binary.Left,\n                        binary.Right));\n\n            case ExpressionType.Power:\n                throw new NotImplementedException(\"Power over non-double operands\");\n\n            case ExpressionType.PowerAssign:\n                return Visit(\n                    Expression.Assign(\n                        binary.Left,\n                        Expression.Power(\n                            binary.Left,\n                            binary.Right)));\n        }\n\n        var liftedStatementOrigPosition = _liftedState.Statements.Count;\n        var left = Translate<ExpressionSyntax>(binary.Left);\n        var liftedStatementLeftPosition = _liftedState.Statements.Count;\n        var right = Translate<ExpressionSyntax>(binary.Right);\n\n        // If both sides were lifted, we don't need to do anything special. Same if the left side was lifted.\n        // But if the right side was lifted and the left wasn't, then in order to preserve evaluation order we need to lift the left side\n        // out as well, otherwise the right side gets evaluated before the left.","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L319-L355","documentation":"Thrown by LinqToCSharpSyntaxTranslator.VisitBinary when an ExpressionType.Power binary is encountered whose operands are not both System.Double. Power over double is lowered to a call to Math.Pow(double,double); any other operand type (int, decimal, float, etc.) is unhandled and produces this NotImplementedException.","triggerScenarios":"An expression tree handed to LinqToCSharpSyntaxTranslator contains an Expression.Power node whose Left.Type or Right.Type is not double — e.g. an int power built via Expression.Power(Expression.Constant(2), Expression.Constant(3)), or a decimal power.","commonSituations":"A query-translation extension or a manually-built expression tree uses Expression.Power over integer/decimal operands and then feeds the result to the C# syntax translator (compiled-query source generation); EF provider extensions that materialize Power expressions for non-double numerics.","solutions":["Cast both operands of Expression.Power to double before constructing the Power node, and cast the result back if needed.","Replace Expression.Power with an explicit Expression.Call(typeof(Math), nameof(Math.Pow), null, leftAsDouble, rightAsDouble).","If the integer/decimal power semantics are required, compute the value outside the expression tree and inline the result as a constant."],"exampleFix":"// before\nvar power = Expression.Power(\n    Expression.Constant(2),\n    Expression.Constant(10)); // both int -> throws\n\n// after\nvar power = Expression.Power(\n    Expression.Convert(Expression.Constant(2), typeof(double)),\n    Expression.Convert(Expression.Constant(10), typeof(double)));","handlingStrategy":"validation","validationCode":"// Reject non-double Power operands before translating to C#.\nusing System.Linq.Expressions;\n\nbool IsSupportedPower(BinaryExpression b)\n    => b.NodeType == ExpressionType.Power\n       && b.Left.Type == typeof(double)\n       && b.Right.Type == typeof(double);\n\n// In a visitor, ensure all Power nodes are double-typed before handing the tree off.","typeGuard":"static bool IsDoublePower(BinaryExpression b)\n    => b.NodeType == ExpressionType.Power\n       && b.Left.Type == typeof(double)\n       && b.Right.Type == typeof(double);","tryCatchPattern":null,"preventionTips":["Always convert Power operands to double before constructing Expression.Power.","If you need integer/decimal exponentiation, compute the value out-of-band and inline the result.","Add a pre-translation visitor that asserts every Power node has double operands."],"tags":["ef-core","expression-trees","linq-expressions","code-generation","math","csharp"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}