Share via


集計操作

集計操作では、複数の値のコレクションから単一の値が計算されます。 集計操作の例として、1 か月間の毎日の気温から平均気温を計算する操作があります。

次の図は、数値のシーケンスに対して 2 つの異なる集計操作を実行した結果を示しています。 最初の操作では、数値の合計が計算されます。 2 番目の操作では、シーケンスの値の中から最大値が返されます。

LINQ 集計操作

次のセクションに、集計操作を実行する標準クエリ演算子のメソッドの一覧を示します。

メソッド

メソッド名

Description

C# のクエリ式の構文

Visual Basic のクエリ式の構文

詳細情報

Aggregate

コレクションの値に対して、カスタム集計操作を実行します。

該当なし

該当なし

Enumerable.Aggregate``1

Queryable.Aggregate``1

Average

値のコレクションの平均値を計算します。

該当なし

Aggregate … In … Into Average()

Enumerable.Average

Queryable.Average

[カウント]

コレクション内の要素数をカウントします。オプションとして、述語関数を満たす要素だけをカウントすることもできます。

該当なし

Aggregate … In … Into Count()

Enumerable.Count``1

Queryable.Count``1

LongCount

大規模なコレクション内の要素数をカウントします。オプションとして、述語関数を満たす要素だけをカウントすることもできます。

該当なし

Aggregate … In … Into LongCount()

Enumerable.LongCount``1

Queryable.LongCount``1

最大

コレクション内の最大値を判定します。

該当なし

Aggregate … In … Into Max()

Enumerable.Max

Queryable.Max``1

最小

コレクション内の最小値を判定します。

該当なし

Aggregate … In … Into Min()

Enumerable.Min

Queryable.Min``1

Sum

コレクション内の値の合計を計算します。

該当なし

Aggregate … In … Into Sum()

Enumerable.Sum

Queryable.Sum

クエリ式の構文の例

Average

Visual Basic で Aggregate Into Average 句を使用して、気温を表す数値の配列から平均気温を計算する方法を次のコード例に示します。

        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

[カウント]

Visual Basic で Aggregate Into Count 句を使用して、配列内にある 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

Visual Basic で Aggregate Into LongCount 句を使用して、配列内の値の数をカウントする方法を次のコード例に示します。

        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

最大

Visual Basic で Aggregate Into Max 句を使用して、気温を表す数値の配列から最高気温を求める方法を次のコード例に示します。

        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

最小

Visual Basic で Aggregate Into Min 句を使用して、気温を表す数値の配列から最低気温を求める方法を次のコード例に示します。

        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

Visual Basic で Aggregate Into Sum 句を使用して、費用を表す値の配列から総費用を計算する方法を次のコード例に示します。

        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

参照

処理手順

方法: CSV テキスト ファイルの列値を計算する (LINQ)

方法 : LINQ を使用したデータの数、合計、または平均の算出 (Visual Basic)

方法 : LINQ を使用したクエリ結果内の最小値と最大値の検索 (Visual Basic)

方法: ディレクトリ ツリー内で最もサイズの大きいファイルを照会する (LINQ)

方法 : 一連のフォルダーの合計バイト数を問い合わせる (LINQ)

関連項目

Aggregate 句 (Visual Basic)

System.Linq

概念

標準クエリ演算子の概要