简体   繁体   中英

How to implement SQL Server paging using C# and entity framework?

I have to update every row in a Sql Server table with about 150,000 records using entity framework. To reduce the amount of hits the server takes, I would like to do this in separate batches of 1000 rows. I need entity framework to:

  1. Select the first 1000 rows from the DB.
  2. Update those rows.
  3. Call SaveChanges() method.
  4. Get next 1000 rows.
  5. Repeat.

Whats the best way to achieve this?

I'm using entity framework 4 and SQL Server 2012.

Use LINQ Skip & Take :

return query.Skip(HOW MUCH TO SKIP -AT THE BEGINNING WILL BE ZERO-)
    .Take(HOW MUCH TO TAKE -THE NUMBER OF YOUR PAGING SIZE-).ToList();

If you want to do it within a loop you can do something like this:

int pagingIncrement = 1000;
for (int i = 0; i <= 150 000; i=i+pagingIncrement)
{
    var query = your actual LINQ query.
    var results = query.Skip(i).Take(pagingIncrement);

    UpdatePartialResults(results);
}

Note: It is important that while updating those rows you don't update the criteria for the ORDER BY within your actual LINQ query, otherwise you could be end up updating the same results again and again (because of the reordering).

Other idea will be to extend the IEnumerable iterator with some of the previously given ideas such as a Skip(counter).Take(pagingSize and yield result (to be processing kinda asynchronously).

something like this should work:

int skip =0;
int take = 1000;
for (int i = 0; i < 150; i++)
{
var rows = (from x in Context.Table
            select x).OrderBy(x => x.id).Skip(skip).Take(take).ToList();

//do some update stuff with rows

skip += 1000;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM