A lot of the LINQ to SQL that I’ve been doing involves reading data from a table and then calling the ToList() extension method and using the data in memory. However, I now want to use LINQ to SQL to process more records that can fit in memory. This is the pattern that I’ve come up with so far:
int skip = 0;
IList<Record> records = new List<Record>();
do
{
records = DBRecords.Skip(skip).Take(1000).Select(a => new Record
{
// Set values here...
}).ToList();
foreach (Record r in records)
{
yield return r;
}
skip += 1000;
} while (records.Count > 0);
This allows me to pull 1000 records at a time and return them in batches to the app. However, I know that there must be a better way of doing this?
linq is lazy evaluated, however ToList() will realize the whole result. You can probably just drop the ToList() and return the result of your query directly. Processing the result in the caller in a foreach block will do so lazily.