【问题标题】:ToListAsync() vs AsDocumentQuery()ToListAsync() 与 AsDocumentQuery()
【发布时间】:2022-11-12 19:37:19
【问题描述】:

我刚刚阅读了这个section,它显示了这个例子:

IDocumentQuery<dynamic> query = client.CreateDocumentQuery(
    UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName), 
    "SELECT * FROM c WHERE c.city = 'Seattle'", 
    new FeedOptions 
    { 
        PartitionKey = new PartitionKey("Washington")
    }).AsDocumentQuery();
while (query.HasMoreResults) 
{
    foreach(Document document in await queryable.ExecuteNextAsync())
    {
        // Iterate through documents
    }
}

在我们项目的代码库中,我们正在做这样的事情:

var invoiceList = await CreateDocumentQuery(companyId).Where(i => i.InvoiceId == invoiceId).ToListAsync();

其中CreateDocumentQuery() 返回IOrderedQueryable&lt;Invoice&gt;

使用ToListAsync() 不好吗? 我什么时候应该使用ToListAsync()AsDocumentQuery()/ExecuteNextAsync()

【问题讨论】:

    标签: c# nosql azure-cosmosdb


    【解决方案1】:

    AsDocumentQuery 让您按照自己的节奏使用查询,如果需要分页(返回继续令牌),则让步。

    ToListAsync 一口气将查询完全耗尽。

    这取决于您的用例,哪种模式更适合。我在这里看到的建议:https://learn.microsoft.com/azure/cosmos-db/nosql/troubleshoot-query-performance#common-sdk-issues 是:“一定要完全耗尽查询。”

    ToListAsync 会完全耗尽它,AsDocumentQuery 也会消耗它,直到 HasMoreResultsfalse(就像在你的 sn-p 中一样)。

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多