연산자를 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)
참고하십시오
- 집계 쿼리
- 샘플 데이터베이스 다운로드하는