Rediger

Del via


Aggregation Operations (Visual Basic)

An aggregation operation computes a single value from a collection of values. An example of an aggregation operation is calculating the average daily temperature from a month's worth of daily temperature values.

The following illustration shows the results of two different aggregation operations on a sequence of numbers. The first operation sums the numbers. The second operation returns the maximum value in the sequence.

Illustration that shows LINQ aggregation operations.

The standard query operator methods that perform aggregation operations are listed in the following section.

Methods

Method Name Description Visual Basic Query Expression Syntax More Information
Aggregate Performs a custom aggregation operation on the values of a collection. Not applicable. Enumerable.Aggregate

Queryable.Aggregate
Average Calculates the average value of a collection of values. Aggregate … In … Into Average() Enumerable.Average

Queryable.Average
Count Counts the elements in a collection, optionally only those elements that satisfy a predicate function. Aggregate … In … Into Count() Enumerable.Count

Queryable.Count
LongCount Counts the elements in a large collection, optionally only those elements that satisfy a predicate function. Aggregate … In … Into LongCount() Enumerable.LongCount

Queryable.LongCount
Max or MaxBy Determines the maximum value in a collection. Aggregate … In … Into Max() Enumerable.Max
Enumerable.MaxBy
Queryable.Max
Queryable.MaxBy
Min or MinBy Determines the minimum value in a collection. Aggregate … In … Into Min() Enumerable.Min
Enumerable.MinBy
Queryable.Min
Queryable.MinBy
Sum Calculates the sum of the values in a collection. Aggregate … In … Into Sum() Enumerable.Sum

Queryable.Sum

Query Expression Syntax Examples

Average

The following code example uses the Aggregate Into Average clause in Visual Basic to calculate the average temperature in an array of numbers that represent temperatures.


Dim temperatures() As Double = {72.0, 81.5, 69.3, 88.6, 80.0, 68.5}

Dim avg = Aggregate temp In temperatures Into Average()

' Display the result.
MsgBox(avg)

' This code produces the following output:

' 76.65

Count

The following code example uses the Aggregate Into Count clause in Visual Basic to count the number of values in an array that are greater than or equal to 80.


Dim temperatures() As Double = {72.0, 81.5, 69.3, 88.6, 80.0, 68.5}

Dim highTemps As Integer = Aggregate temp In temperatures Into Count(temp >= 80)

' Display the result.
MsgBox(highTemps)

' This code produces the following output:

' 3

LongCount

The following code example uses the Aggregate Into LongCount clause to count the number of values in an array.


Dim temperatures() As Double = {72.0, 81.5, 69.3, 88.6, 80.0, 68.5}

Dim numTemps As Long = Aggregate temp In temperatures Into LongCount()

' Display the result.
MsgBox(numTemps)

' This code produces the following output:

' 6

Max

The following code example uses the Aggregate Into Max clause to calculate the maximum temperature in an array of numbers that represent temperatures.


Dim temperatures() As Double = {72.0, 81.5, 69.3, 88.6, 80.0, 68.5}

Dim maxTemp = Aggregate temp In temperatures Into Max()

' Display the result.
MsgBox(maxTemp)

' This code produces the following output:

' 88.6

Min

The following code example uses the Aggregate Into Min clause to calculate the minimum temperature in an array of numbers that represent temperatures.


Dim temperatures() As Double = {72.0, 81.5, 69.3, 88.6, 80.0, 68.5}

Dim minTemp = Aggregate temp In temperatures Into Min()

' Display the result.
MsgBox(minTemp)

' This code produces the following output:

' 68.5

Sum

The following code example uses the Aggregate Into Sum clause to calculate the total expense amount from an array of values that represent expenses.


Dim expenses() As Double = {560.0, 300.0, 1080.5, 29.95, 64.75, 200.0}

Dim totalExpense = Aggregate expense In expenses Into Sum()

' Display the result.
MsgBox(totalExpense)

' This code produces the following output:

' 2235.2

See also