Search code examples
asp.netodata

OData WebAPI: How to hardcode OrderBy in Function?


I am using OData WebAPI (Microsoft.AspNet.OData). for some reasons, I need hardcore OrderBy in a function. for example and return like the following,

 return DbContext.Companies.OrderByDescending(a => a.CompanyName);

When I executed the function, orderby hardcoded did not work, I also checked sqlprofile, it seem framework did something and remove it. If I use $orderby queryoption, it is no problem.

Is there anyone knows how to solve it? thks in advance.


Solution

  • One workaround to implement is by making CompanyName a part of the composite key and specifying the Column attribute. I doubt that it will work without making the property part of the key and just using the Column attribute but you can quickly try that out.

    One other workaround is to insert an orderby clause in ODataQueryOptions in your controller.