{"record":{"id":"55afd36e08d7722d","repo":"devlooped/moq","slug":"the-equals-or-in-vb-and-the-conditional-and-or-andalso-in-vb","errorCode":null,"errorMessage":"The equals (\"==\" or \"=\" in VB) and the conditional 'and' (\"&&\" or \"AndAlso\" in VB) operators are the only ones supported in the query specification expression. Unsupported expression: {0}","messagePattern":"The equals \\(\"==\" or \"=\" in VB\\) and the conditional 'and' \\(\"&&\" or \"AndAlso\" in VB\\) operators are the only ones supported in the query specification expression\\. Unsupported expression: (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/Linq/MockSetupsBuilder.cs","lineNumber":32,"sourceCode":"{\n    class MockSetupsBuilder : ExpressionVisitor\n    {\n        static readonly string[] queryableMethods = new[] { \"First\", \"Where\", \"FirstOrDefault\" };\n        static readonly string[] unsupportedMethods = new[] { \"All\", \"Any\", \"Last\", \"LastOrDefault\", \"Single\", \"SingleOrDefault\" };\n\n        int stackIndex;\n        int quoteDepth;\n\n        public MockSetupsBuilder()\n        {\n        }\n\n        protected override Expression VisitBinary(BinaryExpression node)\n        {\n            if (this.stackIndex > 0)\n            {\n                if (node.NodeType != ExpressionType.Equal && node.NodeType != ExpressionType.AndAlso)\n                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.LinqBinaryOperatorNotSupported, node.ToStringFixed()));\n\n                if (node.NodeType == ExpressionType.Equal)\n                {\n                    // TODO: throw if a matcher is used on either side of the expression.\n                    //ThrowIfMatcherIsUsed(\n\n                    // Account for the inverted assignment/querying like \"false == foo.IsValid\" scenario\n                    if (node.Left.NodeType == ExpressionType.Constant)\n                        // Invert left & right nodes in this case.\n                        return ConvertToSetup(node.Right, node.Left) ?? base.VisitBinary(node);\n                    else\n                        // Perform straight conversion where the right-hand side will be the setup return value.\n                        return ConvertToSetup(node.Left, node.Right) ?? base.VisitBinary(node);\n                }\n            }\n\n            return base.VisitBinary(node);\n        }","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Linq/MockSetupsBuilder.cs#L14-L50","documentation":"Moq's LINQ-to-Mocks (Mock.Of / query syntax) translates the query expression into mock setups via MockSetupsBuilder.VisitBinary. Inside a specification, only Equal (==) and AndAlso (&&) binary operators can be translated; anything else (>, <, !=, ||, +, etc.) throws NotSupportedException. This is a deliberate limitation of the simple LINQ provider, not a bug.","triggerScenarios":"var foo = Mock.Of<IFoo>(f => f.Bar > 3), f => f.Name != \"x\", or f => f.A || f.B — any query-spec binary node whose NodeType is neither Equal nor AndAlso when stackIndex > 0.","commonSituations":"Writing convenient range or inequality filters in Mock.Of predicates; using || for 'or' conditions; after migrating from a full LINQ provider and assuming all operators are supported.","solutions":["Rewrite the predicate using only == and && (e.g. replace x > 3 with an exact-value match).","Invert logic: replace 'a != b' with 'a == expected' where possible.","Fall back to classic setup API: mock.Setup(f => f.Bar).Returns(...).Callback(...) or Setup with It.Is<T>(predicate) matchers, which support arbitrary predicates.","Use a callback/Returns delegate for complex logic instead of the LINQ query expression."],"exampleFix":"// before\nvar foo = Mock.Of<IFoo>(f => f.Bar > 3);\n// after\nvar foo = Mock.Of<IFoo>(f => f.Bar == 4);\n// or\nvar mock = new Mock<IFoo>();\nmock.Setup(f => f.Bar).Callback(() => { /* complex logic */ }).Returns(5);","handlingStrategy":"validation","validationCode":"// Restrict Mock.Of predicates to == and &&:\n// OK: f => f.A == 1 && f.B == \"x\"\n// NOT OK: f => f.A > 1 || f.B != \"x\"  -> use mock.Setup + It.Is instead","typeGuard":null,"tryCatchPattern":"try { var foo = Mock.Of<IFoo>(predicate); }\ncatch (NotSupportedException) { var mock = new Mock<IFoo>(); mock.Setup(...); foo = mock.Object; }","preventionTips":["Remember LINQ-to-Mocks supports only == and && inside the specification.","For inequalities, ranges, or || conditions, use mock.Setup with It.Is<T>(predicate) matchers.","Keep Mock.Of predicates trivial; move complex conditions into setup API."],"tags":["moq","linq","query-syntax","unsupported-operator","csharp"],"backgroundTag":"unsupported-operation","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}