{"record":{"id":"2ed70dbfa24249d8","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-collectionselector-observable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'collectionSelector')","messagePattern":"Value cannot be null\\. \\(Parameter 'collectionSelector'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs","lineNumber":1190,"sourceCode":"        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TCollection\">The type of the elements in the projected intermediate sequences.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate sequence elements.</typeparam>\n        /// <param name=\"source\">An observable sequence of elements to project.</param>\n        /// <param name=\"collectionSelector\">A transform function to apply to each element.</param>\n        /// <param name=\"resultSelector\">A transform function to apply to each element of the intermediate sequence.</param>\n        /// <returns>An observable sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of the input sequence and then mapping each of those sequence elements and their corresponding source element to a result element.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"collectionSelector\"/> or <paramref name=\"resultSelector\"/> is null.</exception>\n        public static IObservable<TResult> SelectMany<TSource, TCollection, TResult>(this IObservable<TSource> source, Func<TSource, IObservable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (collectionSelector == null)\n            {\n                throw new ArgumentNullException(nameof(collectionSelector));\n            }\n\n            if (resultSelector == null)\n            {\n                throw new ArgumentNullException(nameof(resultSelector));\n            }\n\n            return s_impl.SelectMany(source, collectionSelector, resultSelector);\n        }\n\n        /// <summary>\n        /// Projects each element of an observable sequence to an observable sequence by incorporating the element's index, invokes the result selector for the source element and each of the corresponding inner sequence's elements, and merges the results into one observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TCollection\">The type of the elements in the projected intermediate sequences.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate sequence elements.</typeparam>\n        /// <param name=\"source\">An observable sequence of elements to project.</param>\n        /// <param name=\"collectionSelector\">A transform function to apply to each element; the second parameter of the function represents the index of the source element.</param>","sourceCodeStart":1172,"sourceCodeEnd":1208,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs#L1172-L1208","documentation":"In this SelectMany overload the collectionSelector (Func<TSource, IObservable<TCollection>>) determines the inner sequence for each element; it must not be null. The method validates all three arguments eagerly and throws ArgumentNullException naming 'collectionSelector' when it is null.","triggerScenarios":"source.SelectMany(null, (x, c) => result) or passing a null delegate variable into the collectionSelector slot while resultSelector is valid. Thrown at Observable.StandardSequenceOperators.cs:1190.","commonSituations":"Query-builder patterns where the collection selector is composed conditionally and ends up null; DI/config-injected projection functions that failed to resolve; refactors renaming a method but leaving a null delegate reference.","solutions":["Provide a real collection selector: source.SelectMany(x => x.Children.ToObservable(), (x, c) => ...).","Guard dynamic delegates: if (collectionSelector == null) collectionSelector = x => Observable.Empty<TCollection>();","Confirm the argument order — collectionSelector comes before resultSelector; a swapped/null argument can trigger this."],"exampleFix":"// before\nFunc<Order, IObservable<Item>> items = null;\nvar q = orders.SelectMany(items, (o, i) => i.Price);\n\n// after\nvar q = orders.SelectMany(o => o.Items.ToObservable(), (o, i) => i.Price);","handlingStrategy":"validation","validationCode":"if (collectionSelector is null)\n    collectionSelector = static _ => Observable.Empty<TCollection>();","typeGuard":"static bool HasCollSelector<TSrc, TColl>(Func<TSrc, IObservable<TColl>> sel) => sel is not null;","tryCatchPattern":"try\n{\n    var q = source.SelectMany(collectionSelector, resultSelector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"collectionSelector\")\n{\n    throw new InvalidOperationException(\"Collection selector missing in SelectMany composition\", ex);\n}","preventionTips":["Inline the collection selector lambda at the call site whenever possible","For dynamic query builders, require non-null delegates in the builder's Build() step","Watch argument order: collectionSelector before resultSelector"],"tags":["rx","argument-null","selectmany","delegate"],"backgroundTag":"null-argument","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"}