Home > Software engineering >  Cosmos DB pagination with sorting
Cosmos DB pagination with sorting

Time:05-13

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!

CodePudding user response:

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)
            
  • Related