Search code examples
asp.net-web-apiodata

WebApi reordering results?


I hav a problem with WebApi and OData. Slowly moving an API over and... now it seems that the framework is reordering the results.

The following code:

    [EnableQuery(PageSize = 100, MaxTop = 1000, AllowedQueryOptions = AllowedQueryOptions.All)]
    [ODataRoute]
    public IEnumerable<Reflexo.Api.GrdJob> Get(ODataQueryOptions options) {
        var nodes = Repository.GrdJob
            .Include(x=>x.Cluster)
            .OrderByDescending(x => x.Id)
            .Select(x => new Reflexo.Api.GrdJob() {
                Id = x.Id,
                Identity = x.Code,
            }).AsQueryable();
        nodes = (IQueryable<Reflexo.Api.GrdJob>)options.ApplyTo(nodes);
        var retval = nodes.ToArray();
        return nodes;
    }

is as simlple as it gets. Comparing the results in the debugger with what I can see on the screen calling the method... the results have a different order.

Note that I am comparing the db side id fields (id) of both the JSON I see in a browser and the fields in the array named retval. I have imposed an artificial default order - which also get into the SQL (checked) and array (checked).

Just the JSON shows results in a different order.

Am I missing something?


Solution

  • Be aware the EnableQueryAttribute is going to execute ODataQueryOptions.ApplyTo again using a default set of query settings. (See the EnableQueryAttribute.ApplyQuery method source.) Try removing the attribute.