LINQ,Skip and Take Azure SQL数据库不起作用

时间:2013-03-18 18:47:55

标签: asp.net-mvc linq azure-sql-database skip take

我在ASP.NET MVC3应用程序上提取分页数据集,该应用程序使用JQuery通过$ ajax调用获取无限滚动分页的数据。后端是Azure SQL数据库。这是代码:

[Authorize]
[OutputCache(Duration=0,NoStore=true)]
public PartialViewResult Search(int page = 1)
{ 

      int batch = 10; 
      int fromRecord = 1; 
      int toRecord = batch;

      if (page != 1)
      {
         //note these are correctly passed and set
         toRecord = (batch * page);
         fromRecord = (toRecord - (batch - 1));

      }

IQueryable<TheTable> query;

query = context.TheTable.Where(m => m.Username==HttpContext.User.Identity.Name)        
.OrderByDescending(d => d.CreatedOn)
.Skip(fromRecord).Take(toRecord);

//this should always be the batch size (10)
//but seems to concatenate the previous results ???

int count = query.ToList().Count();

//results
//call #1, count = 10
//call #2, count = 20
//call #3, count = 30
//etc...


PartialViewResult newPartialView = PartialView("Dashboard/_DataList", query.ToList());
return newPartialView;

 }

来自Jquery $ ajax的每次调用返回的数据在每次后续调用时继续为GROW,而不是每次调用仅返回10条记录。因此结果返回包含所有早期的调用数据。另外,我还在$ ajax调用中添加了'cache = false'。关于这里出了什么问题的任何想法?

1 个答案:

答案 0 :(得分:0)

您传递给SkipTake的值是错误的。

  • Skip的参数应该是您要跳过的记录数,在第一页上应为0;

  • Take的参数必须是您要返回的记录数,该记录总是等于batch;

您的代码必须是:

int batch = 10; 
int fromRecord = 0; 
int toRecord = batch;

if (page != 1)
{
   fromRecord = batch * (page - 1);
}