必须在方法'Skip'Exception之前调用'OrderBy'方法

时间:2012-07-21 15:21:14

标签: asp.net-mvc-3 entity-framework linq-to-entities mvcjqgrid

我尝试使用jQgrid来实现MvcjQgrid,我得到了这个例外。

System.NotSupportedException was unhandled by user code
  Message=The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

虽然在Skip方法之前使用了OrdeyBy,但为什么它会产生异常?怎么解决?

我在控制器中遇到了异常:

public ActionResult GridDataBasic(GridSettings gridSettings)
        {          
            var jobdescription = sm.GetJobDescription(gridSettings);
            var totalJobDescription = sm.CountJobDescription(gridSettings);

            var jsonData = new
            {
                total = totalJobDescription / gridSettings.PageSize + 1,
                page = gridSettings.PageIndex,
                records = totalJobDescription,
                rows = (
                    from j in jobdescription
                    select new
                    {
                        id = j.JobDescriptionID,
                        cell = new[] 
                    { 
                        j.JobDescriptionID.ToString(), 
                        j.JobTitle,
                        j.JobType.JobTypeName,
                        j.JobPriority.JobPriorityName,
                        j.JobType.Rate.ToString(),
                        j.CreationDate.ToShortDateString(),
                         j.JobDeadline.ToShortDateString(),

                    }
                    }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

GetJobDescription方法 CountJobDescription方法

public int CountJobDescription(GridSettings gridSettings)
        {
            var jobdescription = _dataContext.JobDescriptions.AsQueryable();

            if (gridSettings.IsSearch)
            {
                jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription);
            }
            return jobdescription.Count();
        }

        public IQueryable<JobDescription> GetJobDescription(GridSettings gridSettings)
        {
            var jobdescription = orderJobDescription(_dataContext.JobDescriptions.AsQueryable(), gridSettings.SortColumn, gridSettings.SortOrder);

            if (gridSettings.IsSearch)
            {
                jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription);
            }

            return jobdescription.Skip((gridSettings.PageIndex - 1) * gridSettings.PageSize).Take(gridSettings.PageSize);
        }

最后 FilterJobDescription和OrderJobDescription

private static IQueryable<JobDescription> FilterJobDescription(IQueryable<JobDescription> jobdescriptions, Rule rule)
        {
            if (rule.field == "JobDescriptionID")
            {
                int result;
                if (!int.TryParse(rule.data, out result))
                    return jobdescriptions;
                return jobdescriptions.Where(j => j.JobDescriptionID == Convert.ToInt32(rule.data));

            }

// Similar Statements

            return jobdescriptions;
        }



private IQueryable<JobDescription> orderJobDescription(IQueryable<JobDescription> jobdescriptions, string sortColumn, string sortOrder)
        {
            if (sortColumn == "JobDescriptionID")
                return (sortOrder == "desc") ? jobdescriptions.OrderByDescending(j => j.JobDescriptionID) : jobdescriptions.OrderBy(j => j.JobDescriptionID);

            return jobdescriptions;
        }

2 个答案:

答案 0 :(得分:14)

该异常意味着如果您应用Skip始终需要排序输入,同样在用户未点击要排序的列的情况下也是如此。我可以想象,在用户甚至可以单击列标题之前,第一次打开网格视图时没有指定排序列。为了捕捉这种情况,我建议在没有给出其他排序标准时定义一些你想要的默认排序,例如:

switch (sortColumn)
{
    case "JobDescriptionID":
        return (sortOrder == "desc")
            ? jobdescriptions.OrderByDescending(j => j.JobDescriptionID)
            : jobdescriptions.OrderBy(j => j.JobDescriptionID);

    case "JobDescriptionTitle":
        return (sortOrder == "desc")
            ? jobdescriptions.OrderByDescending(j => j.JobDescriptionTitle)
            : jobdescriptions.OrderBy(j => j.JobDescriptionTitle);

    // etc.

    default:
        return jobdescriptions.OrderBy(j => j.JobDescriptionID);
}

修改

根据您的评论关于您的后续问题:您无法在LINQ to Entities查询中使用ToString()。接下来的问题是您无法在查询中创建string数组。我建议使用本机类型从数据库加载数据,然后在内存中转换为字符串(和字符串数组):

rows = (from j in jobdescription
        select new
        {
            JobDescriptionID = j.JobDescriptionID,
            JobTitle = j.JobTitle,
            JobTypeName = j.JobType.JobTypeName,
            JobPriorityName = j.JobPriority.JobPriorityName,
            Rate = j.JobType.Rate,
            CreationDate = j.CreationDate,
            JobDeadline = j.JobDeadline
        })
        .AsEnumerable() // DB query runs here, the rest is in memory
        .Select(a => new
        {
            id = a.JobDescriptionID,
            cell = new[] 
            { 
                a.JobDescriptionID.ToString(), 
                a.JobTitle,
                a.JobTypeName,
                a.JobPriorityName,
                a.Rate.ToString(),
                a.CreationDate.ToShortDateString(),
                a.JobDeadline.ToShortDateString()
            }
        })
        .ToArray()

答案 1 :(得分:3)

在使用 Adam Anderson 中的一些代码进行排序后,我遇到了同样类型的问题,该代码接受了OrderBy中的泛型排序字符串。

在得到这个重复之后,我做了很多研究并发现了非常聪明的解决方法:

var query = SelectOrders(companyNo, sortExpression);

return Queryable.Skip(query, iStartRow).Take(iPageSize).ToList();

希望有所帮助!

SP