0

我们网站的搜索功能之一返回的结果太多,无法处理一页,因此我尝试添加此处提供的分页功能:https ://github.com/TroyGoode/PagedList

该解决方案可以正确构建并且页面也将加载,但是当我尝试进行搜索时,页面的控制器/Index() 方法会抛出“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 指向 return 语句。这只是我在 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));
4

2 回答 2

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);
于 2013-06-10T14:38:44.820 回答
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,但是这在 Union 语句中也是必需的。最后的“return”语句如下:

return View((lastAgents.Union(corp2Agents)).OrderBy(s => s.sNumber).ToPagedList(pageNumber, pageSize));
于 2012-12-18T22:00:51.837 回答