{"record":{"id":"5c922e93becd522c","repo":"dotnet/efcore","slug":"unsupported-linq-unary-node-unary-nodetype","errorCode":null,"errorMessage":"Unsupported LINQ unary node: {unary.NodeType}","messagePattern":"Unsupported LINQ unary node: (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2513,"sourceCode":"            ExpressionType.IsTrue => operand,\n            ExpressionType.ArrayLength => _g.MemberAccessExpression(operand, \"Length\"),\n            ExpressionType.Convert => ParenthesizedExpression((ExpressionSyntax)_g.ConvertExpression(Generate(unary.Type), operand)),\n            ExpressionType.ConvertChecked =>\n                ParenthesizedExpression((ExpressionSyntax)_g.ConvertExpression(Generate(unary.Type), operand)),\n            ExpressionType.Throw when unary.Type == typeof(void) => _g.ThrowStatement(operand),\n            ExpressionType.Throw => _g.ThrowExpression(operand),\n            ExpressionType.TypeAs => BinaryExpression(SyntaxKind.AsExpression, operand, Generate(unary.Type)),\n            ExpressionType.Quote => operand,\n            ExpressionType.UnaryPlus => PrefixUnaryExpression(SyntaxKind.UnaryPlusExpression, operand),\n            ExpressionType.Unbox => operand,\n            ExpressionType.Increment => Translate(Expression.Add(unary.Operand, Expression.Constant(1))),\n            ExpressionType.Decrement => Translate(Expression.Subtract(unary.Operand, Expression.Constant(1))),\n            ExpressionType.PostIncrementAssign => PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, operand),\n            ExpressionType.PostDecrementAssign => PostfixUnaryExpression(SyntaxKind.PostDecrementExpression, operand),\n            ExpressionType.PreIncrementAssign => PrefixUnaryExpression(SyntaxKind.PreIncrementExpression, operand),\n            ExpressionType.PreDecrementAssign => PrefixUnaryExpression(SyntaxKind.PreDecrementExpression, operand),\n\n            _ => throw new ArgumentOutOfRangeException(\"Unsupported LINQ unary node: \" + unary.NodeType)\n        };\n\n        return unary;\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitMemberInit(MemberInitExpression memberInit)\n    {\n        var objectCreation = Translate<ObjectCreationExpressionSyntax>(memberInit.NewExpression);\n\n        List<MemberListBinding>? incompatibleListBindings = null;\n\n        var initializerExpressions = new List<AssignmentExpressionSyntax>(memberInit.Bindings.Count);\n\n        foreach (var binding in memberInit.Bindings)\n        {\n            // C# collection initialization syntax only works when Add is called on an IEnumerable, but LINQ supports arbitrary add\n            // methods. Skip these, we'll add them later outside the initializer","sourceCodeStart":2495,"sourceCodeEnd":2531,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2495-L2531","documentation":"Thrown by VisitUnary as the default case of its NodeType switch when a UnaryExpression node type is not among the handled set (Negate, NegateChecked, Not, OnesComplement, IsFalse, IsTrue, ArrayLength, Convert, ConvertChecked, Throw, TypeAs, Quote, UnaryPlus, Unbox, Increment, Decrement, and the increment/decrement assignment types). It is an ArgumentOutOfRangeException indicating the node type is simply not implemented by the translator.","triggerScenarios":"An expression tree contains a UnaryExpression whose NodeType is outside the enumerated supported set (for example an assignment-related or arithmetic-assignment unary type the translator never added), and the tree reaches the C# translator.","commonSituations":"Programmatic construction of unusual UnaryExpression node types; expression trees produced by reflection over arithmetic-assignment operations; edge-case LINQ shapes in precompiled queries.","solutions":["Rewrite the expression to use a supported unary node type or an equivalent binary/method-call form.","Avoid arithmetic-assignment unary nodes (e.g. AddAssign-like unary forms) in translated trees.","Check the supported NodeType list in VisitUnary and restructure accordingly."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"static readonly HashSet<ExpressionType> SupportedUnary = new()\n{ ExpressionType.Negate, ExpressionType.NegateChecked, ExpressionType.Not, ExpressionType.OnesComplement,\n  ExpressionType.IsFalse, ExpressionType.IsTrue, ExpressionType.ArrayLength, ExpressionType.Convert,\n  ExpressionType.ConvertChecked, ExpressionType.Throw, ExpressionType.TypeAs, ExpressionType.Quote,\n  ExpressionType.UnaryPlus, ExpressionType.Unbox, ExpressionType.Increment, ExpressionType.Decrement,\n  ExpressionType.PostIncrementAssign, ExpressionType.PostDecrementAssign, ExpressionType.PreIncrementAssign, ExpressionType.PreDecrementAssign };\nstatic bool UsesOnlySupportedUnary(Expression e) { /* visit UnaryExpression nodes, check SupportedUnary.Contains(u.NodeType) */ return true; }","typeGuard":"static bool IsSupportedUnaryType(ExpressionType t) => SupportedUnary.Contains(t);","tryCatchPattern":"try { var csharp = translator.Translate(expr); }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Unsupported LINQ unary node\"))\n{ throw new InvalidOperationException(\"Query uses an unimplemented unary node type.\", ex); }","preventionTips":["Check node types against the supported set before translating.","Rewrite arithmetic-assignment unary nodes as binary/method-call expressions.","Keep query expressions simple to stay within supported shapes."],"tags":["expression-trees","precompiled-queries","compiled-model","linq","unary-operator"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}