The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

Jul 25, 2014 at 6:45 PM
Edited Jul 25, 2014 at 6:47 PM
Basically I am trying to do server side paging.

I get an error (above) when trying to do the stuff below.
How can I set the orderBy ?




protected void Page_Load(object sender, EventArgs e)
    {
        grdAccountList.DataSource = accountService.Query()
                                    .SelectPage(1, grdAccountList.SettingsPager.PageSize, out _recordCount)
                                    .ToList();
        grdAccountList.DataBind();
    }
protected void grdAccountList_PageIndexChanged(object sender, EventArgs e)
    {
        grdAccountList.DataSource = accountService.Query()
                                    .SelectPage(grdAccountList.PageIndex, grdAccountList.SettingsPager.PageSize, out _recordCount)
                                    .ToList();
        grdAccountList.DataBind();
    }
Jul 28, 2014 at 9:20 AM
Edited Jul 28, 2014 at 9:21 AM
You must use OrderBy before SelectPage.
grdAccountList.DataSource = 
    accountService.Query()
    .OrderBy(queryable => queryable.OrderBy(account => account.Id))
    .SelectPage(grdAccountList.PageIndex, grdAccountList.SettingsPager.PageSize, out _recordCount)
    .ToList();
A Consideration

This implicit behaviour is not very intuitive. Since SelectPage performs a Skip and OrderBy must be called before the method Skip, SelectPage should ensure an OrderBy has already been established. Ideally, SelectPage should only be avaliable after an OrderBy has been used in the method chaining.
Jul 28, 2014 at 1:43 PM
Thanks a lot emperor_ming.

I mostly had it right, except that I need to brush up on LINQ. I didn' t know that I could do this :
".OrderBy(account => account.Id))"

....thanks
Coordinator
Aug 1, 2014 at 6:44 PM
Thanks, will consider this (API improvement) for our next release.
Marked as answer by lelong37 on 8/1/2014 at 10:44 AM