집계 작업은 값 컬렉션에서 단일 값을 계산합니다. 집계 작업의 예는 한 달 분량의 일일 온도 값에서 평균 일일 온도를 계산하는 것입니다.
다음 그림에서는 숫자 시퀀스에 대한 두 가지 집계 작업의 결과를 보여 줍니다. 첫 번째 연산은 숫자를 합산합니다. 두 번째 작업은 시퀀스의 최대값을 반환합니다.
집계 작업을 수행하는 표준 쿼리 연산자 메서드는 다음 섹션에 나열됩니다.
메서드
메서드 이름 | 설명 | Visual Basic 쿼리 식 구문 | 더 많은 정보 |
---|---|---|---|
집계 | 컬렉션 값에 대한 사용자 지정 집계 작업을 수행합니다. | 적용할 수 없습니다. | Enumerable.Aggregate Queryable.Aggregate |
평균 | 값 컬렉션의 평균 값을 계산합니다. | Aggregate … In … Into Average() |
Enumerable.Average Queryable.Average |
수량 | 조건자 함수를 충족하는 요소만 선택적으로 컬렉션의 요소 수를 계산합니다. | Aggregate … In … Into Count() |
Enumerable.Count Queryable.Count |
롱카운트 | 큰 컬렉션에서 요소를 계산할 때, 선택적으로 조건자 함수를 충족하는 요소만 세어볼 수 있습니다. | Aggregate … In … Into LongCount() |
Enumerable.LongCount Queryable.LongCount |
Max 또는 MaxBy | 컬렉션의 최대값을 결정합니다. | Aggregate … In … Into Max() |
Enumerable.Max Enumerable.MaxBy Queryable.Max Queryable.MaxBy |
Min 또는 MinBy | 컬렉션의 최소값을 결정합니다. | Aggregate … In … Into Min() |
Enumerable.Min Enumerable.MinBy Queryable.Min Queryable.MinBy |
합계 | 컬렉션에 있는 값의 합계를 계산합니다. | Aggregate … In … Into Sum() |
Enumerable.Sum Queryable.Sum |
쿼리 식 구문 예제
평균
다음 코드 예제에서는 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
롱카운트
다음 코드 예제에서는 절을 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
맥스
다음 코드 예제에서는 절을 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
민
다음 코드 예제에서는 절을 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
합계
다음 코드 예제에서는 이 절을 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
참고하십시오
.NET