{"record":{"id":"0265cbac34011e5c","repo":"dotnet/reactive","slug":"strings-linq-no-elements-querylanguage-blocking","errorCode":null,"errorMessage":"Strings_Linq.NO_ELEMENTS","messagePattern":"Strings_Linq\\.NO_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Blocking.cs","lineNumber":85,"sourceCode":"        {\n            return FirstOrDefault(Where(source, predicate));\n        }\n\n        [return: MaybeNull]\n        private static TSource FirstOrDefaultInternal<TSource>(IObservable<TSource> source, bool throwOnEmpty)\n        {\n            using var consumer = new FirstBlocking<TSource>();\n\n            using (source.Subscribe(consumer))\n            {\n                consumer.Wait();\n            }\n\n            consumer._error?.Throw();\n\n            if (throwOnEmpty && !consumer._hasValue)\n            {\n                throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);\n            }\n\n            return consumer._value;\n        }\n\n        #endregion\n\n        #region + ForEach +\n\n        public virtual void ForEach<TSource>(IObservable<TSource> source, Action<TSource> onNext)\n        {\n            using var sink = new ForEach<TSource>.Observer(onNext);\n\n            using (source.SubscribeSafe(sink))\n            {\n                sink.Wait();\n            }\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Blocking.cs#L67-L103","documentation":"Blocking First/FirstOrDefault (via FirstOrDefaultInternal) drains the queryable sequence and throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the sequence completed without emitting any value and a default is not allowed (throwOnEmpty). It represents 'no elements' rather than an error in the underlying stream.","triggerScenarios":"Calling queryable.First() or FirstOrDefault(source => predicate, throwOnEmpty semantics) on an empty IQbservable, or where the OnError path stored an exception that was rethrown and the sequence produced no value.","commonSituations":"Waiting for the first result of a remote queryable stream that produced no data (empty DB table, filter matched nothing); race where the source completed before subscription processed a value.","solutions":["Use FirstOrDefault() to receive default(T) instead of throwing when the sequence may be empty","Check whether the source sequence is expected to be non-empty and fix the producer","Catch InvalidOperationException around the blocking call and treat it as an empty-result condition"],"exampleFix":"// before\nvar first = queryable.First(); // throws on empty\n// after\nvar first = queryable.FirstOrDefault(); // default(T) when empty","handlingStrategy":"try-catch","validationCode":"// cannot know emptiness before subscribing; prefer the non-throwing API instead","typeGuard":null,"tryCatchPattern":"try { var v = queryable.First(); } catch (InvalidOperationException) { /* sequence was empty */ var v = default(T); }","preventionTips":["Prefer FirstOrDefault() over First() when empty is possible","Confirm the remote source actually yields data before blocking","Handle empty results as a normal outcome, not an error"],"tags":["empty-sequence","blocking","rx","first"],"backgroundTag":"empty-result-set","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"}