为什么没有CreateDocumentQuery的异步版本?
例如,此方法可以是异步的:
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;
}
Run Code Online (Sandbox Code Playgroud)
Kas*_*ikh 16
好查询,
只需尝试以下代码即可以异步方式使用它.
DocumentQueryable.CreateDocumentQuery方法为集合下的文档创建查询.
// 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据Kasam Shaikh的回答,我创建了ToListAsync扩展
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();
}
Run Code Online (Sandbox Code Playgroud)
你可以用
var propertiesOfUser = await
client.CreateDocumentQuery<Property>(_collectionLink)
.Where(p => p.OwnerId == userGuid)
.ToListAsync()
Run Code Online (Sandbox Code Playgroud)
请注意,在github上打开了CreateDocumentQuery async的请求
| 归档时间: |
|
| 查看次数: |
9878 次 |
| 最近记录: |