This is a migrated thread and some comments may be shown as answers.
ToDataSourceResult doesn't preserve the default order when 'sort' http variable is not set
1 Answer 108 Views
This is a migrated thread and some comments may be shown as answers.
Vasili
Top achievements
Rank 1
Vasili asked on 10 Feb 2013, 05:02 AM
Hi,
I'm using EF and I want to add a default order for items in a grid. To do that I've added a call to OrderBy and after that I convert the result to DataSourceResult:

var data = context.Subjects.OrderBy(x => x.ScreeningNumber).ToDataSourceResult(request);

The problem is that if there is no order set (this.HttpContext.Request.Form["sort"] is "") I expect it to use my default ordering, but for some reason ToDataSourceResult completely removes ordering. How can I keep my default order in such cases?

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 13 Feb 2013, 05:41 AM
Hello Vasili,

The order is lost because the ToDataSourceResult method will apply order on the first column when Entity Framework is used and there are not any SortDescriptors. This is needed because Entity Framework always requires order in order to perform the paging. In order to set the order in this scenario, I can suggest to check if there are not any SortDescriptors and add one for the property:

if (request.Sorts.Count == 0)
{
    request.Sorts.Add(new SortDescriptor("ScreeningNumber",
        System.ComponentModel.ListSortDirection.Ascending));             
}

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Vasili
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or