Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetQueryAsync ignores existing entity OrderBy expression if $orderby is not present in ODataQueryOptions but $skip and/or $top are #213

Open
Kizzik opened this issue May 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Kizzik
Copy link

Kizzik commented May 28, 2024

Version: 5.0.0 (EFCore)

Expected behavior

Results are sorted using the .OrderBy expression.

Actual behavior

A default sort order is applied.

Steps to reproduce

Make an .OrderBy call before .GetQueryAsync, with $top and/or $skip specified. e.g.:

// url: https://myhost/api/myEntities?$top=20&$skip=0
var baseQuery = context.MyEntities.OrderBy(e => /* custom sort */);
var query = await baseQuery.GetQueryAsync(mapper, opts);

Details

I have an ordering requirement that I can't feasibly represent using OData, so my intent is to order the IQueryable before calling GetQueryAsync.

This works if I don't use OData $skip or $top, but if either of those are specified then the custom ordering is overridden by the GetQueryableMethod > GetDefaultOrderByCall call.

I've tried doing the .OrderBy call after .GetQueryAsync, however if the expression used for my custom order includes navigation properties/children that aren't selected/expanded in the ODataQueryOptions (which I don't want, as I don't want to return the sorting data), then it fails.

Is there a way to bypass the initialisation of the default ordering, or have it detect that we already have an OrderBy call in the expression and use that in lieu of seeking a default? My current solution involves creating a new instance of ODataQueryOptions with top/skip removed, applying my custom sort, calling GetQueryAsync, then applying top/skip myself, but it's an ugly hack.

@BlaiseD
Copy link
Member

BlaiseD commented May 29, 2024

I think there was a PR to do exactly that i.e. if there's a Skip but no OrderBy then add one. Adding a setting to ODataSattings something like SkipDefaultOrderBy should work.

@BlaiseD BlaiseD added the enhancement New feature or request label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants