6.8. Perform Count Operations

Problem

You need to count the number of elements within a collection or array.

Solution

Create an Aggregate query, covered in recipe 6-6, and use the Count or LongCount function, within the Into clause.

How It Works

Recipe 6-6 details the use of the Aggregate...Into clause. This clause is used to perform some calculation over a series of data. The Into clause is used to specify the calculation that is to be performed.

If you need to count all the elements in a series, you use either the Count or LongCount function, such as this:

Dim cnt = Aggregate book In books _
          Into Count(book.Price = 49.99)

This will return an Integer value that represents the count of all elements whose Price value is equal to 49.99. The

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.