2017-06-15 61 views
1

對documentDb執行linq計數查詢時可能獲取RequestCharge嗎? 如樣品LINQ查詢DocumentDb聚合操作上的RequestCharge

count = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions) 
     .Where(f => f.LastName == "Andersen") 
     .Count(); 

感謝

多納爾

回答

1

是否有可能得到RequestCharge執行對documentDb一個LINQ計數查詢時?

如果要對documentDb使用linq計數查詢,則需要在Count方法之前調用ToList方法。

.ToList().Count(); 

它會發送多個查詢到documentDb服務器並獲取所有文檔。這裏是我在調用ToList()。Count()時在Fiddler中找到的請求列表。我的收藏中只有2個文檔,因此會發送2/dbs/ToDoList/colls/Items請求。

enter image description here

它等於下面的代碼和RequestCharges可以用下面的代碼被添加在一起。

IDocumentQuery<T> query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions) 
    .Where(f => f.LastName == "Andersen") 
    .AsDocumentQuery(); 

List<T> results = new List<T>(); 
double sumRequestCharges = 0; 
while (query.HasMoreResults) 
{ 
    FeedResponse<T> queryResult = await query.ExecuteNextAsync<T>(); 
    sumRequestCharges += queryResult.RequestCharge; 
    results.AddRange(queryResult); 
} 

如果您只是需要查詢文檔的計數,我們建議您使用sql查詢而不是.ToList方法。它不會發送多個請求到documentDb服務器並節省大量時間。以下代碼供您參考。

var query = client.CreateDocumentQuery<object>(
    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new SqlQuerySpec("SELECT count(c.id) FROM c")).AsDocumentQuery(); 
FeedResponse <object> queryResult = await query.ExecuteNextAsync<object>(); 
double requestCharge = queryResult.RequestCharge; 
+0

謝謝Amor我會玩sql選擇查詢,看看會產生什麼。請注意,我正在處理的文檔數據庫查詢示例不使用ToList方法! –

+0

直接調用Count方法會引發異常。如果它支持你,你可以在使用Fiddler調用Count方法時檢查發送了多少請求。 – Amor

+0

嗨,Amor,請參閱此鏈接的DocumentClient使3個調用 - https://stackoverflow.com/questions/44590614/documentdb-query-that-expects-full-results-from-aggregate-functions-is-not-sup –