Azure Cosmos DB to fetch paged results along with all records count

Viewed 244

The document structure is like this

[
  {
    "id": "1234",
    "SubmittedAnswers": [
      {
        "PlantId": 3,
        "UniqueQualityId": "3_pe55d74fc5f92b11ab3fe"
      },
      {
        "PlantId": 4,
        "UniqueQualityId": "3_pe55d74fc5sdfmsdfklms"
      }
    ],
    "CompletionTime": 36
  },
  {
    "id": "1234",
    "SubmittedAnswers": [
      {
        "PlantId": 3,
        "UniqueQualityId": "3_pe55d74fc5f92b11ab3fe"
      },
      {
        "PlantId": 4,
        "UniqueQualityId": "3_pe55d74fc5sdfmsdfklms"
      }
    ],
    "CompletionTime": 36
  }
]

In order to display the data on the grid, I had to fetch only paginated data (10 records), by using continuation token I was able get the paginated data using the following code

QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
QueryRequestOptions options = new QueryRequestOptions()
{
    MaxItemCount = request.RowCount > 0 ? request.RowCount : -1,
    MaxConcurrency = 10
};

FeedIterator<Submit> queryResultSetIterator = 
    cosmosContainer.GetItemQueryIterator<Submit>(queryDefinition,
    requestOptions:options,continuationToken: request.ContinuationToken);
List<Submit> submissions = new List<Submit>();

while (queryResultSetIterator.HasMoreResults)
{
    FeedResponse<Submit> currentResultSet = await queryResultSetIterator.ReadNextAsync();
    continuationToken = currentResultSet.ContinuationToken;
    foreach (Submit survey in currentResultSet)
    {
        submissions.Add(survey);
    }
    if (request.RowCount > 0)
        break;
}
return new Response{ Answers = submissions,ContinuationToken= continuationToken };

Along with these filtered or paginated data,the total records count is also required paginate properly so for that I thought of making another call to fetch only the count of records

select value count(1) from c

Is there any better approach to fetch these paginated results and all records count together

0 Answers
Related