{"record":{"id":"a3ac0a64558f9b0a","repo":"dotnet/reactive","slug":"the-specified-expression-is-not-assignable-to-the-result","errorCode":null,"errorMessage":"The specified expression is not assignable to the result type.","messagePattern":"The specified expression is not assignable to the result type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableQuery.cs","lineNumber":108,"sourceCode":"        }\n\n        /// <summary>\n        /// Executes an expression tree representing a computation over asynchronous enumerable sequences.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the result of evaluating the expression tree.</typeparam>\n        /// <param name=\"expression\">The expression tree to evaluate.</param>\n        /// <param name=\"token\">Cancellation token used to cancel the evaluation.</param>\n        /// <returns>Task representing the result of evaluating the specified expression tree.</returns>\n        ValueTask<TResult> IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken token)\n        {\n            if (expression == null)\n            {\n                throw new ArgumentNullException(nameof(expression));\n            }\n\n            if (!typeof(ValueTask<TResult>).IsAssignableFrom(expression.Type))\n            {\n                throw new ArgumentException(\"The specified expression is not assignable to the result type.\", nameof(expression));\n            }\n\n            return new AsyncEnumerableExecutor<TResult>(expression).ExecuteAsync(token);\n        }\n\n        /// <summary>\n        /// Gets an enumerator to enumerate the elements in the sequence.\n        /// </summary>\n        /// <param name=\"token\">Cancellation token used to cancel the enumeration.</param>\n        /// <returns>A new enumerator instance used to enumerate the elements in the sequence.</returns>\n        public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token)\n        {\n            token.ThrowIfCancellationRequested();\n\n            if (_enumerable == null)\n            {\n                var expression = Expression.Lambda<Func<IAsyncEnumerable<T>>>(new AsyncEnumerableRewriter().Visit(_expression), null);\n                _enumerable = expression.Compile()();","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableQuery.cs#L90-L126","documentation":"IAsyncQueryProvider.ExecuteAsync<TResult> requires the expression's static type to be assignable to ValueTask<TResult>, because execution returns ValueTask<TResult>. It throws ArgumentException(\"The specified expression is not assignable to the result type.\", nameof(expression)) when the expression tree's Type does not match, indicating the composed expression and the requested TResult disagree.","triggerScenarios":"Calling ExecuteAsync<int> with an expression whose Type is ValueTask<string> (or Task<long>, etc.), or composing FirstAsync/SumAsync-style calls where the element type differs from the TResult supplied to the provider.","commonSituations":"Type drift after changing an element type (e.g. int to long) without updating the ExecuteAsync call, custom providers returning plain Task expressions, or generic helpers hardcoding an incorrect TResult.","solutions":["Align TResult with the expression's result type so expression.Type is assignable to ValueTask<TResult>.","Rebuild the expression with the correct return type (e.g. adjust the final Select/cast in the tree).","If you have Task<TResult> instead, adapt it: new ValueTask<TResult>(task) before forming the expression."],"exampleFix":"// before\nvar result = await provider.ExecuteAsync<int>(\n    Expression.Call(sumLongMethod, source.Expression), token); // Type is ValueTask<long>\n// after\nvar result = await provider.ExecuteAsync<long>(\n    Expression.Call(sumLongMethod, source.Expression), token);","handlingStrategy":"type-guard","validationCode":"if (!typeof(ValueTask<TResult>).IsAssignableFrom(expression.Type)) throw new InvalidOperationException($\"expression type {expression.Type} not assignable to ValueTask<{typeof(TResult)}>\");","typeGuard":"bool IsAssignableToResult<T>(Expression e) => typeof(ValueTask<T>).IsAssignableFrom(e.Type);","tryCatchPattern":"try { var r = await provider.ExecuteAsync<T>(expr, token); } catch (ArgumentException ex) when (ex.ParamName == \"expression\") { // rebuild with corrected TResult r = await provider.ExecuteAsync<CorrectType>(expr, token); }","preventionTips":["Derive TResult from expression.Type instead of hardcoding it in generic helpers.","Re-check result types after changing element types in the query.","Add a debug assertion comparing expression.Type against ValueTask<TResult> before execution."],"tags":["type-mismatch","async","linq"],"backgroundTag":"type-mismatch","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}