按页面大小检索记录

时间:2013-02-05 14:55:58

标签: linq sql-server-2008 entity-framework c#-4.0

如果我使用普通查询,它有一个大表,它有一个超时异常。所以我想选择前1000然后输出它,下一步是从1001检索到2000并记录它,依此类推。

我不确定如何在查询中添加参数。

int pageNumer = 0;
var query = DBContext.MyTable.Where(c=>c.FacilityID == facilityID)
 .OrderBy(c=>c.FilePath)
 .Skip(pageNumer*1000)
 .Take(1000);

foreach(var x in query)
{
     // Console.WriteLine(x.Name);
}
   // I want pageNumber is incremented until it goes to the bottom of the table.
   // I don't know how many records in the table.

1 个答案:

答案 0 :(得分:0)

试试这个:

int pageNumber = 0;
bool hasHitEnd = false;

while (!hasHitEnd)
{
   var query = DBContext.MyTable.Where(c=>c.FacilityID == facilityID)
    .OrderBy(c=>c.FilePath)
    .Skip(pageNumber*1000)
    .Take(1000);

   foreach(var x in query)
   {
        // Do something
   }

   if (query.Count < 1000)
   {
      hasHitEnd = true;
   }

   pageNumber++;
}