WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

DocumentDB now has .Take() / $top= support, but OData attempts to sort by primary key when applying $top to DocumentDB's IQueryable

Open nickkin-msft opened this issue 10 years ago • 2 comments

Example query: http://localhost/odata/MyEntitySet?$top=1

Example controller:

    [EnableQuery]
    public async Task<IQueryable<MyEntityDto>>Get()
    {
        /* (set up DocumentDB client) */

        var queryable = Client.CreateDocumentQuery<MyEntityDto>("dbs/" + Database.Id + "/colls/" + Collection.Id);

        return queryable;
    }

Resulting error:

    A first chance exception of type 'Microsoft.Azure.Documents.BadRequestException' occurred in Microsoft.Azure.Documents.Client.dll

    Additional information: Message: {"Errors":["Primary key order-by is not supported."]}

Edit: fixed formatting, also you should note that you'll need the very latest client sdk for this, 1.5.2

Edit 2: for anyone who finds this via a search engine, I found that this behavior can be disabled with an option: https://msdn.microsoft.com/en-us/library/system.web.http.odata.query.odataquerysettings.ensurestableordering(v=vs.118).aspx which resolves the problem.

nickkin-msft avatar Dec 16 '15 03:12 nickkin-msft

Hi @nickkin-msft it is by design that we will order the result by primary key if your apply top or skip, relative code is in https://github.com/OData/WebApi/blob/master/OData/src/System.Web.OData/OData/Query/ODataQueryOptions.cs#L318-L331

VikingsFan avatar Dec 21 '15 02:12 VikingsFan

I found that this behavior can be disabled with an option: https://msdn.microsoft.com/en-us/library/system.web.http.odata.query.odataquerysettings.ensurestableordering(v=vs.118).aspx which resolves the problem.

You can close this bug. Thanks!

nickkin-msft avatar Dec 25 '15 18:12 nickkin-msft