The method 'OrderBy' must be called before the method 'Skip' Exception

后端 未结 2 1340
南笙
南笙 2021-02-08 04:58

I was trying to implement the jQgrid using MvcjQgrid and i got this exception.

System.NotSupportedException was unhandled by user code         


        
相关标签:
2条回答
  • 2021-02-08 05:54

    The exception means that you always need a sorted input if you apply Skip, also in the case that the user doesn't click on a column to sort by. I could imagine that no sort column is specified when you open the grid view for the first time before the user can even click on a column header. To catch this case I would suggest to define some default sorting that you want when no other sorting criterion is given, for example:

    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);
    }
    

    Edit

    About your follow-up problems according to your comment: You cannot use ToString() in a LINQ to Entities query. And the next problem would be that you cannot create a string array in a query. I would suggest to load the data from the DB with their native types and then convert afterwards to strings (and to the string array) in memory:

    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()
    
    0 讨论(0)
  • 2021-02-08 05:59

    I had the same type of problem after sorting using some code from Adam Anderson that accepted a generic sort string in OrderBy.

    After getting this excpetion, i did lots of research and found that very clever fix:

    var query = SelectOrders(companyNo, sortExpression);
    
    return Queryable.Skip(query, iStartRow).Take(iPageSize).ToList();
    

    Hope that helps !

    SP

    0 讨论(0)
提交回复
热议问题