方法' Skip'仅支持LINQ to Entities中的排序输入。方法' OrderBy'必须在方法之前调用'跳过'。

时间:2014-09-24 14:20:01

标签: c# asp.net asp.net-mvc linq entity-framework

我有上述错误而且非常奇怪,因为我没有使用任何' Skip'方法。

我还注意到,我在switch语句中的sortOrder是null。

你能帮我找出造成这个错误的原因吗?

告诉我我的代码:

public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
 ViewBag.CurrentSort = sortOrder;
            ViewBag.BookingIdSortParm = String.IsNullOrEmpty(sortOrder) ? "RecordId" : "";
            ViewBag.ItemSortParm = String.IsNullOrEmpty(sortOrder) ? "Cart" : "";
            ViewBag.UserFullNameSortParm = String.IsNullOrEmpty(sortOrder) ? "UserFullName" : "";
            ViewBag.StatusCodeSortParm = String.IsNullOrEmpty(sortOrder) ? "StatusCode" : "";
            ViewBag.RequestDateSortParm = String.IsNullOrEmpty(sortOrder) ? "RequestDate" : "";
            ViewBag.StartDateSortParm = String.IsNullOrEmpty(sortOrder) ? "StartDate" : "";
            ViewBag.EndDateSortParm = String.IsNullOrEmpty(sortOrder) ? "EndDate" : "";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var myBookingList = (from s in SystemDB.Users                              
                                where s.UserLogin == strLoginName
                                join b in SystemDB.Carts on s.DepartmentId equals b.Booking.Item.DepartmentId 
                                where b.Stage != 1 && b.Stage != 5                              
                                orderby b.RecordId descending
                                select b).GroupBy(b => b.CartId).Select(y => y.FirstOrDefault());



            if (!String.IsNullOrEmpty(searchString))
            {


                myBookingList = myBookingList.Where(s => s.RecordId.ToString().Contains(searchString.ToUpper())
                                       || s.CartId.ToUpper().Contains(searchString.ToUpper())
                                       || s.Booking.UserFullName.ToUpper().Contains(searchString.ToUpper())
                                       || s.StatusCode.StatusCodeName.ToUpper().Contains(searchString.ToUpper())
                                       || s.Booking.RequestDate.ToString().Contains(searchString.ToUpper())
                                       || s.Booking.StartDate.ToString().Contains(searchString.ToUpper())
                                       || s.Booking.EndDate.ToString().Contains(searchString.ToUpper()));
            }

            switch (sortOrder)
            {
                case "BookingId":
                    myBookingList = myBookingList.OrderByDescending(s => s.RecordId);
                    break;

                case "Cart":
                    myBookingList = myBookingList.OrderBy(s => s.CartId);
                    break;

                case "UserFullName":
                    myBookingList = myBookingList.OrderBy(s => s.Booking.UserFullName);
                    break;

                case "StatusCode":
                    myBookingList = myBookingList.OrderBy(s => s.StatusCode.StatusCodeName);
                    break;

                case "RequestDate":
                    myBookingList = myBookingList.OrderByDescending(s => s.Booking.RequestDate);
                    break;

                case "StartDate":
                    myBookingList = myBookingList.OrderByDescending(s => s.Booking.StartDate);
                    break;

                case "EndDate":
                    myBookingList = myBookingList.OrderByDescending(s => s.Booking.EndDate);
                    break;

                //default:
                //    throw new ArgumentException("Bad sort order specified", "sortOrder");

            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);


            return View(myBookingList.ToPagedList(pageNumber, pageSize));
}

2 个答案:

答案 0 :(得分:3)

ToPagedList的实施将使用SkipTake来获取查询的每一页。

答案 1 :(得分:0)

设置默认排序顺序而不是评论它将起作用  而不是这个

//default:
//    throw new ArgumentException("Bad sort order specified", "sortOrder");

尝试此操作或设置一些默认排序

default:
myBookingList = myBookingList.OrderByDescending(s => s.RecordId);
break;

希望有所帮助