DocumentClient CreateDocumentQuery async

Why is there no asynchronous version of CreateDocumentQuery?

This method, for example, can be asynchronous:

using (var client = new DocumentClient(new Uri(endpointUrl), authorizationKey, _connectionPolicy)) { List<Property> propertiesOfUser = client.CreateDocumentQuery<Property>(_collectionLink) .Where(p => p.OwnerId == userGuid) .ToList(); return propertiesOfUser; } 
+6
source share
2 answers

Good request

Just try under the code to be asynchronous.

The DocumentQueryable.CreateDocumentQuery method creates a query for documents in the collection.

  // Query asychronously. using (var client = new DocumentClient(new Uri(endpointUrl), authorizationKey, _connectionPolicy)) { var propertiesOfUser = client.CreateDocumentQuery<Property>(_collectionLink) .Where(p => p.OwnerId == userGuid) .AsDocumentQuery(); // Replaced with ToList() while (propertiesOfUser.HasMoreResults) { foreach(Property p in await propertiesOfUser.ExecuteNextAsync<Property>()) { // Iterate through Property to have List or any other operations } } } 
+11
source

Based on Kasam Shaikh's answer I created ToListAsync extensions

  public static async Task<List<T>> ToListAsync<T>(this IDocumentQuery<T> queryable) { var list = new List<T>(); while (queryable.HasMoreResults) { //Note that ExecuteNextAsync can return many records in each call var response = await queryable.ExecuteNextAsync<T>(); list.AddRange(response); } return list; } public static async Task<List<T>> ToListAsync<T>(this IQueryable<T> query) { return await query.AsDocumentQuery().ToListAsync(); } 

You can use it

 var propertiesOfUser = await client.CreateDocumentQuery<Property>(_collectionLink) .Where(p => p.OwnerId == userGuid) .ToListAsync() 

Note that the request to open async CreateDocumentQuery on github

+1
source

All Articles