I'm trying to fetch every record from a table in a MS SQL database with about 20 million entries via an entity data model. My initial idea was to retrieve the data in chunks, like so:
public IEnumerable<IEnumerable<device>> GetDevicesInChunks(int chunkSize)
{
using (var db = new AccountsEntities())
{
for (int i = 0; i < db.devices.Count(); i += chunkSize)
{
yield return db.devices.Skip(i).Take(chunkSize);
}
}
}
However, it appears that I must call OrderBy before I call Skip, judging by the exception that is thrown when I employ the above method
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method
'OrderBy' must be called before the method 'Skip'.
I'm sure calling OrderBy on every subset of records I retrieve will be costly since the devices are in no particular order - I feel like I'm walking down the wrong path here.
What's the best approach to handling large SQL queries via LINQ?
Whereand filter by a primary key instead ofSkip? Or simplyOrderBythe primary key?OrderBythe clustered index, if one is present on the table; if not,OrderBythe Primary Key ... if you must OrderBy. Seems strange that it's required.