If possible, return the enumerated. Thus, you can continue to scroll forward (and only forward) through an excessive number of lines without having to immediately put it into memory, which most likely will never work. If it runs correctly (it also depends on your data source), you can read and process one row at a time, with little or no memory usage.
There are many ways to do this, but the simplest way that I often use is yield return , which will generate its own state machine.
Entity Framework is currently truly conveying results. The only thing you do not need to do is call ToList() or similar methods that will load all the rows from the database in memory. Just iterate over the results as if they were a regular collection. (For EF6 and older, you can use the AsStreaming() extension method.) If this does not work for you, you can always go back to a DataReader , where you read and return row by row.
source share