DocumentClient CreateDocumentQuery async

FailedUnitTest picture FailedUnitTest · Sep 5, 2016 · Viewed 12.2k times · Source

Why is there no async version of CreateDocumentQuery?

This method for example could of 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;
    }

Answer

Kasam Shaikh picture Kasam Shaikh · Sep 6, 2016

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
     }
}


}