0

I have a gridview that i want to sort. I wrote the following method for it:

private void SortGridView(string sortExpression, string direction)
    {
        var constr = new AdminRequirementEF();
        string sort = string.Concat("it.", sortExpression, " ", direction);
        int pageSize = Convert.ToInt32(ddPageSize.SelectedItem.Text);

        var results = constr.Projects;
        int totalRecords = results.Count();
        this.PopulatePager(totalRecords, pageIndex);

        var sortedResults = constr.Projects.OrderBy(sort).Skip((pageIndex - 1) * pageSize).Take(pageNum).ToList();
        grdMain.DataSource = sortedResults;
        grdMain.DataBind();
    }

The problem is sorting is applied on totalrecords not on per page filtered records. I want to use OrderBy(sort) after applying skip and take but it gives me an error stating skip can not be applied before orderby clause. Any help will be much appreciated.

1
  • Which EF version is this? This should work alright. What does the generated SQL look like? Commented Dec 25, 2013 at 20:56

1 Answer 1

1

You can get the constr.Projects collection sorted on its primary key

var results = constr.Projects.OrderBy(p => p.ProjectId)

and then apply the skip and take on the 'results' collection with sorting.

results = results.Skip((pageIndex - 1) * pageSize).Take(pageNum).OrderBy(sort).ToList();

This way you will get the records for particular page sorted as required.

Sign up to request clarification or add additional context in comments.

1 Comment

No its not working, tried this before.I guess Orderby(string value) cant be applied after Skip-Take.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.