question

AgneteRstad-7143 avatar image
0 Votes"
AgneteRstad-7143 asked NavtejSaini-MSFT answered

How to implement pagination and maxItemCount with Cosmos DB feediterator

I'm trying to write Read method where maxItemCount continuation token is passed as arguments, but I'm not able to find any updated guides on how to implement it. The code I have now looks like this, but I can't believe there's no easier way to implement this basic functionality?

public async Task<(IEnumerable<T> Results, string ContinuationToken)> ReadAsync(Expression<Func<T, bool>> predicate, int limit, string continuationToken)
        {
            var entityList = new List<T>();
            int itemsRemaining = limit;

            do
            {
                var maxItemsCount = Math.Min(itemsRemaining, 100);
                var options = new QueryRequestOptions { MaxItemCount = maxItemsCount };
                var query = _container.GetItemLinqQueryable<T>(true, continuationToken, options).Where(predicate);

                using (var iterator = query.ToFeedIterator())
                {
                    if (iterator.HasMoreResults)
                    {
                        FeedResponse<T> response = await iterator.ReadNextAsync();
                        entityList.AddRange(response.Resource);
                        continuationToken = response.ContinuationToken;

                        if (iterator.ReadNextAsync().Result.Count == 0)
                            continuationToken = null;
                    }
                }
                itemsRemaining -= maxItemsCount;
            }
            while (itemsRemaining > 0);

            return (entityList, continuationToken);
        }

azure-cosmos-db
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anonymous user We are checking this and will get back to you.

0 Votes 0 ·

1 Answer

NavtejSaini-MSFT avatar image
0 Votes"
NavtejSaini-MSFT answered

anonymous user

Please check this example from Stack Overflow. If this doesn't help, please get back to us and we will try to help further.

Regards
Navtej S


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anonymous user - Let us know if you need any further help.

0 Votes 0 ·