I am using OData services for my Web Api to provide pagination to my client app. This is the code for one of my controller methods.
// Get all Categories
[HttpGet]
[ActionName("all")]
public ODataResult<DataAccess.Model.Category> GetCategoryList(ODataQueryOptions options)
{
var categoryList = this.Repository.GetCategories().AsQueryable<Category>();
var results = (options.ApplyTo(categoryList) as IQueryable<DataAccess.Model.Category>);
long count = categoryList.Count();
if (count == 0)
count = 1; // throws up an ArgumentException error if the count is less than 1
return new ODataResult<DataAccess.Model.Category>(results, null, count);
}
But when i use any OData protocols in the api call like api/Category/all?$skip&$top=5 it doesnt return me the exact order in which the 5-10 elements are there in the db (i.e sorted by their primary key=ID). I can fix this by using $orderby=ID for all my API calls, but i want to know if i can avoid it. Am i doing anything wrong here because by default the OData services should return in the same order as they are in the db right? Please let me know if you have any ideas or suggestions.
Thanks