Krzysztof
Krzysztof

Reputation: 192

Cosmos DB pagination with sorting

I`m trying to have pagination and sorting on database side using LINQ. From what I have read Cosmos db doesnt support subqueries meaning that either I can have pagination working or sorting.

My question is I guess that is there any option to surpass that?

That is my code:

var query = documentContainer.GetItemLinqQueryable<CosmosEntity>(true)
                .Where(p => p.PartitionKey == id)
                .Skip(searchRequest.Qualifiers.RecordsPerPage * (searchRequest.Qualifiers.CurrentPage - 1))
                .Take(searchRequest.Qualifiers.RecordsPerPage)
                .OrderBy(x => x.DocumentType)

Unfortunately it returns

OFFSET LIMIT' clause is not supported in subqueries.

Is there any way I could make that work? I guess I can have pagination on CosmosDB and sorting in memory on server side but thats not ideal scenario...

THanks in advance!

Upvotes: 2

Views: 841

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

Can you try this? Include the ORDER BY before skip.

var query = documentContainer.GetItemLinqQueryable<CosmosEntity>(true)
                .Where(p => p.PartitionKey == id)
                .OrderBy(x => x.DocumentType)
                .Skip(searchRequest.Qualifiers.RecordsPerPage * (searchRequest.Qualifiers.CurrentPage - 1))
                .Take(searchRequest.Qualifiers.RecordsPerPage)
            

Upvotes: 3

Related Questions