Bagikan melalui


Operasi Agregasi (Visual Basic)

Operasi agregasi menghitung satu nilai dari kumpulan nilai. Contoh operasi agregasi adalah menghitung suhu harian rata-rata dari nilai suhu harian senilai sebulan.

Ilustrasi berikut menunjukkan hasil dari dua operasi agregasi yang berbeda pada urutan angka. Operasi pertama menjumlahkan angka. Operasi kedua mengembalikan nilai maksimum secara berurutan.

Ilustrasi yang menunjukkan operasi agregasi LINQ.

Metode operator kueri standar yang melakukan operasi agregasi tercantum di bagian berikut.

Metode

Nama Metode Deskripsi Sintaks Ekspresi Kueri Visual Basic Informasi Selengkapnya
Gabungan Melakukan operasi agregasi kustom pada nilai koleksi. Tidak dapat diterapkan. Enumerable.Aggregate

Queryable.Aggregate
Tengah Menghitung nilai rata-rata kumpulan nilai. Aggregate … In … Into Average() Enumerable.Average

Queryable.Average
Jumlah Menghitung elemen dalam koleksi, secara opsional hanya elemen yang memenuhi fungsi predikat. Aggregate … In … Into Count() Enumerable.Count

Queryable.Count
LongCount Menghitung elemen dalam koleksi besar, secara opsional hanya elemen yang memenuhi fungsi predikat. Aggregate … In … Into LongCount() Enumerable.LongCount

Queryable.LongCount
Max atau MaxBy Menentukan nilai maksimum dalam koleksi. Aggregate … In … Into Max() Enumerable.Max
Enumerable.MaxBy
Queryable.Max
Queryable.MaxBy
Min atau MinBy Menentukan nilai minimum dalam koleksi. Aggregate … In … Into Min() Enumerable.Min
Enumerable.MinBy
Queryable.Min
Queryable.MinBy
Jumlah total Menghitung jumlah nilai dalam koleksi. Aggregate … In … Into Sum() Enumerable.Sum

Queryable.Sum

Contoh Sintaks Ekspresi Kueri

Tengah

Contoh kode berikut menggunakan klausul Aggregate Into Average di Visual Basic untuk menghitung suhu rata-rata dalam array angka yang mewakili suhu.


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

Jumlah

Contoh kode berikut menggunakan klausa Aggregate Into Count di Visual Basic untuk menghitung jumlah nilai dalam array yang lebih besar dari atau sama dengan 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

Contoh kode berikut menggunakan klausul Aggregate Into LongCount untuk menghitung jumlah nilai dalam 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

Maks

Contoh kode berikut menggunakan klausul Aggregate Into Max untuk menghitung suhu maksimum dalam array angka yang mewakili suhu.


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

Menit

Contoh kode berikut menggunakan klausul Aggregate Into Min untuk menghitung suhu minimum dalam array angka yang mewakili suhu.


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

Jumlah total

Contoh kode berikut menggunakan klausul Aggregate Into Sum untuk menghitung jumlah total pengeluaran dari array nilai yang mewakili pengeluaran.


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

Lihat juga