11

Why is there no async version of CreateDocumentQuery?

This method for example could have been async:

    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;
    }
1
  • 1
    I think that CreateDocumentQuery simply creates a query and does not execute it. You could probably use ToListAsync instead of ToList to execute the query asynchronously. Commented Sep 5, 2016 at 22:09

2 Answers 2

20

Good query,

Just try below code to have it in async fashion.

DocumentQueryable.CreateDocumentQuery method creates a query for documents under a 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
     }
}


}
Sign up to request clarification or add additional context in comments.

Comments

8

Based on Kasam Shaikh's answer I've 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 request to have CreateDocumentQuery async is open on github

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.