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 527 Views
Grid
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
-1
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!
Mihhail
Top achievements
Rank 1
commented on 30 Oct 2023, 07:56 AM

Hello Daniel,

 

Unfortunately, this doesn't answer the initial concern. There are cases where we need custom ordering, which can only be done by using `OrderBy` on the `IQueryable`. In case this is an EF issue, you could at least provide a flag like `SkipDefaultSorting`. Or better yet inspect the IQueryable. 

 

Best regards,

Mihhail

Neli
Telerik team
commented on 01 Nov 2023, 06:31 AM

Hi Mihhail,

There is such Feature Request logged in our official Feedback Portal. Below you will find a link to the item that I would encourage you to cast a vote for: 

- https://feedback.telerik.com/aspnet-mvc/1399551-the-todatasourceresult-function-replaces-an-existing-sort-on-iqueryable-with-a-default-one-if-the-sorts-list-property-is-empty-in-the-datasourcerequest


Regards,
Neli

Tags
Grid
Asked by
Vasili
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or