我對在 C# 中使用 MongoDB 很陌生。我正在嘗試獲取與特定EmployeeId. 我在 MongoDb 中有這個功能:
db.Tickets.aggregate([
{$match: {employeeId: 3}},
{$count: "count"}
]);
(數字 3 只是一個例子)
這將回傳以下內容:{count: 4},這是正確的。我想在 Visual Studio 中的 C# 上獲得該計數。
到目前為止,我已經在 Visual Studio 中創建了這個方法:
public int GetUserTicketCount(User user)
{
int count = collectionOfTickets.Aggregate()
.Match(x => x.employeeId == user.GetEmployeeId())
.Count()
.FirstOrDefault()
.Count();
}
從我在 SO 和其他論壇中搜索的內容來看,這種方法應該有效;但是我收到一個錯誤“AggregateCountResult不包含定義,并且找不到接受第一個型別引數的Count可訪問擴展(您是否缺少 using 指令或程式集參考?)”。CountAggregateCountResult
我該如何解決這個問題,或者如何在 C# 中獲得計數結果?任何和所有的幫助都非常感謝。
uj5u.com熱心網友回復:
Count 是一個屬性,而不是一個方法。
.Count應該管用。只需洗掉括號。
請參閱此處的官方檔案:http ://rstam.github.io/mongo-csharp-driver/2.5/apidocs/html/T_MongoDB_Driver_AggregateCountResult.htm 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529762.html
