How to: Count the Number of Elements in a Sequence (LINQ to SQL)
Use the Count operator to count the number of elements in a sequence.
Running this query against the Northwind sample database produces an output of 91.
Example
The following example counts the number of Customers in the database.
Dim customerCount = db.Customers.Count()
Console.WriteLine(customerCount)
System.Int32 customerCount = db.Customers.Count();
Console.WriteLine(customerCount);
The following example counts the number of products in the database that have not been discontinued.
Running this example against the Northwind sample database produces an output of 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);
See Also
Concepts
Downloading Sample Databases (LINQ to SQL)