2013-03-18 46 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

相關問題