2012-12-18 43 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); 
相关问题