Search code examples
c#linqentity-frameworkdata-paging

Get data in parts from database


I need get all records from table with Take = 1000 and skip all taken records. How should I implement?

var result = context.MyDB.Where(x=>x.ID = ID).Take(1000);

Solution

  • If you want to take all the records from the db, 1000 at a time, something like this should do the job then.

    var startRecord = 0;
    var records = db.where(x=>x.ID ==ID).Skip(startRecord).Take(1000);
    while (records.Any())
    {
      startRecord += 1000;
      // do something with your records
    
      records = db.where(x=>x.ID ==ID).Skip(startRecord).Take(1000);
    }