2012-12-18 89 views
0

其中一個搜索功能,我們的網站會返回一個頁面來處理太多的結果,所以我嘗試添加由這裏提供的分頁功能:https://github.com/TroyGoode/PagedList使用PagedList分頁功能添加到ASP MVC3網站

該解決方案正確建立和頁面加載爲好,但是當我試圖進行搜索的「NotSupportedException異常」是頁面的控制器/索引()方法拋出:

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. 

Visual Studio 2010和中指向在拋出此異常時返回語句。這只是我在ASP MVC工作的第二天,所以任何建議都是值得歡迎的。謝謝!

  case "name": 
       //if no comma, do a combined search by last name and by corporate name. 
       searchString = searchString.ToUpper(); 

       var lastAgents = 
        db.Agent.OrderBy(s => s.LastName).Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId); 
       //end new code 
       var corp2Agents = 
        db.Agent.OrderBy(s => s.CorporateName).Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification); 
       if ((corp2Agents.Count() == 0) & (lastAgents.Count() == 0)) ViewBag.ErrorMessage = "None found in search for Last Names and Companies beginning with " + search1; 
       else ViewBag.Message = "Results of Last Name and Company Name search. Found " + (corp2Agents.Count() + lastAgents.Count()).ToString(); 

       pageNumber = (page ?? 1); 
       return View(lastAgents.Union(corp2Agents).ToPagedList(pageNumber, pageSize)); 

回答

0

永久,但我找到了答案。這兩個語句

   var lastAgents = 
        db.Agent.OrderBy(s => s.LastName).Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId); 
       //end new code 
       var corp2Agents = 
        db.Agent.OrderBy(s => s.CorporateName).Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification); 

包含一個OrderBy,但是這在聯合聲明中也是必需的。最後的 「返回」 語句如下:

return View((lastAgents.Union(corp2Agents)).OrderBy(s => s.sNumber).ToPagedList(pageNumber, pageSize)); 
0

嘗試添加.OrderBy性(s => s.sNumber)在控制器是這樣的:

var lastAgents = 
        db.Agent.Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId).OrderBy(s => s.sNumber); 
       //end new code 
       var corp2Agents = 
        db.Agent.Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
         a => a.AgentIdentification).OrderBy(s => s.CorporateName); 
相關問題