Problem: I have millions of rows from database to be processed.
I need to implement a method that will return a "stream"(?) of database rows. I don't want to load all of them into memory at once.
I was thinking about returning a lazy IEnumerable<Record> and use yield.
The method would handle loading consecutive records using SqlDataReader.
But what will happen when a client will call .Count() on my IEnumerable? Counting all the records would mean a need to fetch them all.
Is there any good modern way to return a stream of objects not storing all of them in memory, just process one by one? My method should return a stream of records.
It seems like Reactive Extensions might solve the problem for me but I have never used it.
Any ideas?
Thanks