Since LoadItems is a lazy enumeration (uses yield ) and you assign it to a field, this means that every time you enumerate _items , you actually call the loop in LoadItems() to start again, i.e. ( Enumerable.Count creates a new Enumerator each time, which causes the body of the LoadItems to start again). Since you do not re-create the reader each time within the LoadItems , its cursor will be located at the end of the stream, so most likely it will not be able to read more lines. I suspect that it returns null , and your single Item object returned in the second call contains the string null .
Solutions for this would be to "implement" the result of the LoadItems by calling Enumerable.ToList , which will provide you with a specific list:
return _items ?? (_items = LoadItems().ToList());
Or ask the reader to return to the beginning of the stream (if possible) so that LoadItems can run the same way again each time.
But I would recommend that you just get rid of yield ing in this case and return a specific list, since there is little use, so you pay the price of complexity for the lack of winnings.
source share