[英]async query in cosmos db
I'm trying to do a query in cosmos db.我正在尝试在 cosmos db 中进行查询。 I want a async query.我想要一个异步查询。 The query return me only one element.查询只返回一个元素。 In the web I have seen how to do it when query return more than one element.在网上,我已经看到了当查询返回多个元素时如何做到这一点。 This is my method:这是我的方法:
public async Task<TenantDetails> ReadBrokerSettings(string tenantId)
{
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
var tenantDTO = this._client.CreateDocumentQuery<TenantDTO>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions)
.Where(f => f.tenantId == tenantId)
.AsEnumerable()
.SingleOrDefault();
return tenantDTO != null ? _iTenantAssembler.DtoToEntity(tenantDTO) : null;
}
Compiler say me than it isn't async.编译器说我不是异步的。 Thanks谢谢
That's because it's not async.那是因为它不是异步的。 You need to convert it to a document query and use the HasMoreResults
+ ExecuteNextAsync
combination and use the await
keyword on the ExecuteNextAsync
.您需要将其转换为文档查询并使用HasMoreResults
+ ExecuteNextAsync
组合并在ExecuteNextAsync
上使用await
关键字。
Here is a working example:这是一个工作示例:
public async Task<TenantDetails> ReadBrokerSettings(string tenantId)
{
FeedOptions queryOptions = new FeedOptions { MaxItemCount = 1 };
var query = this._client.CreateDocumentQuery<TenantDTO>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions)
.Where(f => f.tenantId == tenantId).AsDocumentQuery();
while(query.HasMoreResults)
{
var results = await query.ExecuteNextAsync();
if(results.Any())
{
return _iTenantAssembler.DtoToEntity(results.Single());
}
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.