共用方式為


計算序列中的項目數目

使用 Count 運算子計算序列中的項目數。

如果對 Northwind 範例資料庫執行這個查詢,會產生輸出 91

範例 1

下列範例會計算資料庫中的 Customers 數目。

System.Int32 customerCount = db.Customers.Count();
Console.WriteLine(customerCount);
Dim customerCount = db.Customers.Count()
Console.WriteLine(customerCount)

範例 2

下列範例計算資料庫中未停產的產品項目。

如果對 Northwind 範例資料庫執行這個範例,會產生輸出 69

System.Int32 notDiscontinuedCount =
    (from prod in db.Products
    where !prod.Discontinued
    select prod)
    .Count();

Console.WriteLine(notDiscontinuedCount);
Dim notDiscontinuedCount = Aggregate prod In db.Products _
                           Into Count(Not prod.Discontinued)

Console.WriteLine(notDiscontinuedCount)

另請參閱