I am trying to return paging results the request from CosmosDB. I saw this example from here but I am not sure what to do with the response variable.
// Fetch query results 10 at a time.var queryable = client.CreateDocumentQuery<Book>(collectionLink, new FeedOptions { MaxItemCount = 10 });while (queryable.HasResults){ FeedResponse<Book> response = await queryable.ExecuteNext<Book>();}Am I suppose to return it directly? Or do I have to do something further with the response variable? I tried to return the response variable directly and it's not working. Here's my code:
public async Task<IEnumerable<T>> RunQueryAsync(string queryString) { var feedOptions = new FeedOptions { MaxItemCount = 3 }; IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions); IDocumentQuery<T> query = filter.AsDocumentQuery(); var response = new FeedResponse<T>(); while (query.HasMoreResults) { response = await query.ExecuteNextAsync<T>(); } return response; }Update: After reading @Evandro Paula's answer, I followed the URL and changed my implementation to below. But it is still giving me 500 status code:
public async Task<IEnumerable<T>> RunQueryAsync(string queryString) { var feedOptions = new FeedOptions { MaxItemCount = 1 }; IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions); IDocumentQuery<T> query = filter.AsDocumentQuery(); List<T> results = new List<T>(); while (query.HasMoreResults) { foreach (T t in await query.ExecuteNextAsync()) { results.Add(t); } } return results; }And here's the exception message:
Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception., Windows/10.0.17134 documentdb-netcore-sdk/1.9.1
Update 2: I added the EnableCrossPartitionQuery to true and I am able to get the response from CosmosDB. But I am not able to get the 1 item that I defined. Instead, I got 11 items.