2013-03-18 70 views
0

我拉一个ASP.NET MVC3应用程序的分页数据集,它使用JQuery通过$ ajax调用获取无数滚动分页的数据。后端是一个Azure SQL数据库。下面是代码:LINQ,跳过并采取对抗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的$阿贾克斯每个调用返回的持续增长上的每个后续调用而然后返回每次通话只有10条记录。所以结果返回也包含所有早期的调用数据。另外,我还为$ ajax调用添加了'cache = false'。关于这里出了什么问题的任何想法?

回答

0

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

  • Skip的参数应该是要跳过的记录数,这应该是0的第一页;

  • Take的参数需要是您想要返回的记录数,它始终等于batch;

你的代码需要:

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

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

感谢理查德!我感谢您的帮助。 – 2013-03-18 19:27:03