Поделиться через


Aggregation Operations

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.

LINQ Aggregation Operations

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

Methods

Method Name

Description

C# Query Expression Syntax

Visual Basic Query Expression Syntax

More Information

Aggregate

Performs a custom aggregation operation on the values of a collection.

Not applicable.

Not applicable.

Enumerable.Aggregate

Queryable.Aggregate

Average

Calculates the average value of a collection of values.

Not applicable.

Aggregate … In … Into Average()

Enumerable.Average

Queryable.Average

Count

Counts the elements in a collection, optionally only those elements that satisfy a predicate function.

Not applicable.

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.

Not applicable.

Aggregate … In … Into LongCount()

Enumerable.LongCount

Queryable.LongCount

Max

Determines the maximum value in a collection.

Not applicable.

Aggregate … In … Into Max()

Enumerable.Max

Queryable.Max

Min

Determines the minimum value in a collection.

Not applicable.

Aggregate … In … Into Min()

Enumerable.Min

Queryable.Min

Sum

Calculates the sum of the values in a collection.

Not applicable.

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 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

See Also

Tasks

How to: Compute Column Values in a CSV Text File (LINQ)

How to: Count, Sum, or Average Data by Using LINQ (Visual Basic)

How to: Find the Minimum or Maximum Value in a Query Result by Using LINQ (Visual Basic)

How to: Query for the Largest File or Files in a Directory Tree (LINQ)

How to: Query for the Total Number of Bytes in a Set of Folders (LINQ)

Reference

Aggregate Clause (Visual Basic)

System.Linq

Concepts

Standard Query Operators Overview