使用 Count 運算子計算序列中的項目數。
如果對 Northwind 範例資料庫執行這個查詢,會產生輸出 91。
範例
下列範例會計算資料庫中的 Customers 數目。
Dim customerCount = db.Customers.Count()
Console.WriteLine(customerCount)
System.Int32 customerCount = db.Customers.Count();
Console.WriteLine(customerCount);
下列範例計算資料庫中未停產的產品項目。
如果對 Northwind 範例資料庫執行這個範例,會產生輸出 69。
Dim notDiscontinuedCount = Aggregate prod In db.Products _
Into Count(Not prod.Discontinued)
Console.WriteLine(notDiscontinuedCount)
System.Int32 notDiscontinuedCount =
(from prod in db.Products
where !prod.Discontinued
select prod)
.Count();
Console.WriteLine(notDiscontinuedCount);