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.
.png)
The standard query operator methods that perform aggregation operations are listed in the following section.
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 in Visual Basic 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 in Visual Basic 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 in Visual Basic 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 in Visual Basic 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
More About How to Perform Aggregation Operations
Concepts
Reference