{"record":{"id":"e709645dc9484939","repo":"devlooped/moq","slug":"linq-method-0-not-supported","errorCode":null,"errorMessage":"LINQ method '{0}' not supported.","messagePattern":"LINQ method '(.+?)' not supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/Linq/MockSetupsBuilder.cs","lineNumber":74,"sourceCode":"                return ConvertToSetupReturns(node, Expression.Constant(true));\n            }\n\n            return base.VisitMember(node);\n        }\n\n        protected override Expression VisitMethodCall(MethodCallExpression node)\n        {\n            if (node.Method.DeclaringType == typeof(Queryable) && queryableMethods.Contains(node.Method.Name))\n            {\n                this.stackIndex++;\n                var result = base.VisitMethodCall(node);\n                this.stackIndex--;\n                return result;\n            }\n\n            if (unsupportedMethods.Contains(node.Method.Name))\n            {\n                throw new NotSupportedException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.LinqMethodNotSupported,\n                    node.Method.Name));\n            }\n\n            if (this.stackIndex > 0 && node.Type == typeof(bool))\n            {\n                return ConvertToSetupReturns(node, Expression.Constant(true));\n            }\n\n            return base.VisitMethodCall(node);\n        }\n\n        protected override Expression VisitUnary(UnaryExpression node)\n        {\n            if (this.stackIndex > 0 && node.NodeType == ExpressionType.Not)\n            {\n                return ConvertToSetup(node.Operand, Expression.Constant(false)) ?? base.VisitUnary(node);","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Linq/MockSetupsBuilder.cs#L56-L92","documentation":"When building mock setups from a LINQ query expression, MockSetupsBuilder.VisitMethodCall rejects calls to LINQ methods listed in its unsupportedMethods set (e.g. Count(), Contains(), Where(), Select(), First(), Any(), Skip(), Take()). Such method calls cannot be translated into a single mock setup, so Moq throws NotSupportedException with 'LINQ method {0} not supported.'","triggerScenarios":"Mock.Of<IFoo>(f => f.Items.Count() == 2) or f => f.List.Contains(x) or f => f.Children.Any(c => c.X) — VisitMethodCall sees node.Method.Name in the unsupported list and throws NotSupportedException.","commonSituations":"Trying to express collection-shape expectations in query syntax; checking Count()/Any() on mocked collection properties in Mock.Of; copying LINQ-to-Objects predicates into LINQ-to-Mocks specifications.","solutions":["Replace the LINQ method with a property or indexer comparison the provider understands (e.g. f.Items.Length == 2 for arrays).","Use the standard setup API: mock.Setup(f => f.Items).Returns(items) and let the code under test exercise the real LINQ operators on the returned collection.","Express membership with element equality (f.Items[0] == x) or restructure the specification to avoid Contains/Any/Count inside the query.","Use It.Is<T>(predicate) matchers in mock.Setup for complex conditions."],"exampleFix":"// before\nvar foo = Mock.Of<IFoo>(f => f.Items.Count() == 2);\n// after\nvar mock = new Mock<IFoo>();\nmock.Setup(f => f.Items).Returns(new[] { item1, item2 });","handlingStrategy":"validation","validationCode":"// Do not call LINQ operators (Count, Contains, Any, Where, First, ...) inside Mock.Of specifications.\n// Validate: predicate body should only contain property/indexer access and ==/&&.","typeGuard":null,"tryCatchPattern":"try { var foo = Mock.Of<IFoo>(f => f.Items.Count() == 2); }\ncatch (NotSupportedException) { var mock = new Mock<IFoo>(); mock.Setup(f => f.Items).Returns(items); foo = mock.Object; }","preventionTips":["Return fully-populated collections from mock.Setup(...).Returns(...) instead of asserting on collection LINQ operators.","Use It.Is<T>(x => x.Count() == 2) as a matcher argument, not inside query syntax.","Treat Mock.Of as an exact-property-shape tool only; anything involving LINQ methods belongs in the setup API."],"tags":["moq","linq","query-syntax","unsupported-method","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"}