{"record":{"id":"ff22f5cc94941723","repo":"dotnet/reactive","slug":"invalid-expression-tree-type","errorCode":null,"errorMessage":"Invalid expression tree type.","messagePattern":"Invalid expression tree type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/ObservableQuery.cs","lineNumber":30,"sourceCode":"using System.Reflection;\n\nnamespace System.Reactive\n{\n#if HAS_TRIMMABILITY_ATTRIBUTES\n    [RequiresUnreferencedCode(Constants_Core.AsQueryableTrimIncompatibilityMessage)]\n#endif\n    internal class ObservableQueryProvider : IQbservableProvider, IQueryProvider\n    {\n        public IQbservable<TResult> CreateQuery<TResult>(Expression expression)\n        {\n            if (expression == null)\n            {\n                throw new ArgumentNullException(nameof(expression));\n            }\n\n            if (!typeof(IObservable<TResult>).IsAssignableFrom(expression.Type))\n            {\n                throw new ArgumentException(Strings_Providers.INVALID_TREE_TYPE, nameof(expression));\n            }\n\n            return new ObservableQuery<TResult>(expression);\n        }\n\n        IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)\n        {\n            //\n            // Here we're on the edge between IQbservable and IQueryable for the local\n            // execution case. E.g.:\n            //\n            //   observable.AsQbservable().<operators>.ToQueryable()\n            //\n            // This should be turned into a local execution, with the push-to-pull\n            // adapter in the middle, so we rewrite to:\n            //\n            //   observable.AsQbservable().<operators>.ToEnumerable().AsQueryable()\n            //","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/ObservableQuery.cs#L12-L48","documentation":"ObservableQueryProvider.CreateQuery<TResult> verifies that the expression tree's static Type is assignable to IObservable<TResult> and throws ArgumentException (INVALID_TREE_TYPE, message 'Invalid expression tree type.') otherwise. Qbservable providers can only execute queries rooted in an observable sequence.","triggerScenarios":"Calling CreateQuery<TResult>(expression) (ObservableQuery.cs:24) with an expression whose Type is not IObservable<TResult> — e.g. an expression representing IQueryable, a raw value, or a different generic result type than requested.","commonSituations":"Mixing IQueryable and IQbservable trees (passing an EF/IQueryable expression into a Qbservable provider); calling CreateQuery<int> on a tree typed IObservable<long>; hand-built expression trees with the wrong Type.","solutions":["Build the query from an expression rooted at an IQbservable/IObservable<TResult> matching the requested TResult","Fix the generic type parameter so TResult matches the observable element type of the expression","If porting LINQ-to-Objects code, rewrite the query against the Qbservable provider's AsQbservable entry point instead of passing IQueryable expressions"],"exampleFix":"// before\nExpression expr = Expression.Constant(list); // System.Collections.Generic.List<int>\nvar q = provider.CreateQuery<int>(expr);\n// after\nExpression expr = Expression.Constant(observableInts); // IObservable<int>\nvar q = provider.CreateQuery<int>(expr);","handlingStrategy":"validation","validationCode":"if (expression != null && !typeof(IObservable<TResult>).IsAssignableFrom(expression.Type)) throw new InvalidOperationException(\"expression tree must be rooted in IObservable<T>\");","typeGuard":null,"tryCatchPattern":"try { provider.CreateQuery<T>(expression); } catch (ArgumentException ex) { /* rebuild tree with correct IObservable<T> root */ }","preventionTips":["Keep IQbservable and IQueryable pipelines separate","Verify expression.Type matches the requested TResult before CreateQuery"],"tags":["argument","expression-tree","type-mismatch","rx","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}