Enumerable.Aggregate 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
오버로드
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) |
시퀀스에 누적기 함수를 적용합니다. 지정된 시드 값은 초기 누적기 값으로 사용되고 지정된 함수는 결과 값을 선택하는 데 사용됩니다. |
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) |
시퀀스에 누적기 함수를 적용합니다. 지정된 시드 값은 초기 누적기 값으로 사용됩니다. |
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) |
시퀀스에 누적기 함수를 적용합니다. |
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
시퀀스에 누적기 함수를 적용합니다. 지정된 시드 값은 초기 누적기 값으로 사용되고 지정된 함수는 결과 값을 선택하는 데 사용됩니다.
public:
generic <typename TSource, typename TAccumulate, typename TResult>
[System::Runtime::CompilerServices::Extension]
static TResult Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> ^ func, Func<TAccumulate, TResult> ^ resultSelector);
public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> * Func<'Accumulate, 'Result> -> 'Result
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate, TResult) (source As IEnumerable(Of TSource), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate), resultSelector As Func(Of TAccumulate, TResult)) As TResult
형식 매개 변수
- TSource
source
요소의 형식입니다.
- TAccumulate
누적기 값의 형식입니다.
- TResult
결과 값의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
집계할 IEnumerable<T>입니다.
- seed
- TAccumulate
초기 누적기 값입니다.
- func
- Func<TAccumulate,TSource,TAccumulate>
각 요소에 대해 호출할 누적기 함수입니다.
- resultSelector
- Func<TAccumulate,TResult>
최종 누적기 값을 결과 값으로 변환하는 함수입니다.
반환
- TResult
변환된 최종 누적기 값입니다.
예외
source
, func
또는 resultSelector
가 null
인 경우
예제
다음 코드 예제에서는 누적기 함수 및 결과 선택기를 적용하는 데 사용하는 Aggregate 방법을 보여 줍니다.
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
// Determine whether any string in the array is longer than "banana".
string longestName =
fruits.Aggregate("banana",
(longest, next) =>
next.Length > longest.Length ? next : longest,
// Return the final result as an upper case string.
fruit => fruit.ToUpper());
Console.WriteLine(
"The fruit with the longest name is {0}.",
longestName);
// This code produces the following output:
//
// The fruit with the longest name is PASSIONFRUIT.
Sub AggregateEx3()
Dim fruits() As String =
{"apple", "mango", "orange", "passionfruit", "grape"}
' Determine whether any string in the array is longer than "banana".
Dim longestName As String =
fruits.Aggregate("banana",
Function(ByVal longest, ByVal fruit) _
IIf(fruit.Length > longest.Length, fruit, longest),
Function(ByVal fruit) fruit.ToUpper())
' Display the output.
Console.WriteLine($"The fruit with the longest name is {longestName}")
End Sub
' This code produces the following output:
'
' The fruit with the longest name is PASSIONFRUIT
설명
이 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 메서드를 사용하면 값 시퀀스에 대해 계산을 간단하게 수행할 수 있습니다. 이 메서드는 .에서 각 요소에 대해 한 번 호출 func
하여 작동합니다 source
. 호출 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 될 때마다 func
시퀀스의 요소와 집계된 값(첫 번째 인수로)을 func
모두 전달합니다. 매개 변수의 seed
값은 초기 집계 값으로 사용됩니다. 결과는 func
이전 집계된 값을 대체합니다. 최종 결과는 .의 func
최종 결과를 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)얻기 위해 resultSelector
전달됩니다.
일반적인 집계 작업을 간소화하기 위해 표준 쿼리 연산자는 범용 개수 메서드Count와 4개의 숫자 집계 메서드, 즉 Min, MaxSum및 Average.
적용 대상
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
시퀀스에 누적기 함수를 적용합니다. 지정된 시드 값은 초기 누적기 값으로 사용됩니다.
public:
generic <typename TSource, typename TAccumulate>
[System::Runtime::CompilerServices::Extension]
static TAccumulate Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> ^ func);
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> -> 'Accumulate
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate) (source As IEnumerable(Of TSource), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate)) As TAccumulate
형식 매개 변수
- TSource
source
요소의 형식입니다.
- TAccumulate
누적기 값의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
집계할 IEnumerable<T>입니다.
- seed
- TAccumulate
초기 누적기 값입니다.
- func
- Func<TAccumulate,TSource,TAccumulate>
각 요소에 대해 호출할 누적기 함수입니다.
반환
- TAccumulate
최종 누적기 값입니다.
예외
source
또는 func
가 null
인 경우
예제
다음 코드 예제에서는 누적기 함수를 Aggregate 적용하고 시드 값을 사용하는 방법을 보여 줍니다.
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
// Count the even numbers in the array, using a seed value of 0.
int numEven = ints.Aggregate(0, (total, next) =>
next % 2 == 0 ? total + 1 : total);
Console.WriteLine("The number of even integers is: {0}", numEven);
// This code produces the following output:
//
// The number of even integers is: 6
Sub AggregateEx2()
' Create an array of Integers.
Dim ints() As Integer = {4, 8, 8, 3, 9, 0, 7, 8, 2}
' Count the even numbers in the array, using a seed value of 0.
Dim numEven As Integer =
ints.Aggregate(0,
Function(ByVal total, ByVal number) _
IIf(number Mod 2 = 0, total + 1, total))
' Display the output.
Console.WriteLine($"The number of even integers is {numEven}")
End Sub
' This code produces the following output:
'
'The number of even integers is 6
설명
이 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 메서드를 사용하면 값 시퀀스에 대해 계산을 간단하게 수행할 수 있습니다. 이 메서드는 .에서 각 요소에 대해 한 번 호출 func
하여 작동합니다 source
. 호출 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 될 때마다 func
시퀀스의 요소와 집계된 값(첫 번째 인수로)을 func
모두 전달합니다. 매개 변수의 seed
값은 초기 집계 값으로 사용됩니다. 결과는 func
이전 집계된 값을 대체합니다. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 의 최종 결과를 반환합니다 func
.
일반적인 집계 작업을 간소화하기 위해 표준 쿼리 연산자는 범용 개수 메서드Count와 4개의 숫자 집계 메서드, 즉 Min, MaxSum및 Average.
적용 대상
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
시퀀스에 누적기 함수를 적용합니다.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TSource, TSource> ^ func);
public static TSource Aggregate<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TSource,TSource> func);
static member Aggregate : seq<'Source> * Func<'Source, 'Source, 'Source> -> 'Source
<Extension()>
Public Function Aggregate(Of TSource) (source As IEnumerable(Of TSource), func As Func(Of TSource, TSource, TSource)) As TSource
형식 매개 변수
- TSource
source
요소의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
집계할 IEnumerable<T>입니다.
- func
- Func<TSource,TSource,TSource>
각 요소에 대해 호출할 누적기 함수입니다.
반환
- TSource
최종 누적기 값입니다.
예외
source
또는 func
가 null
인 경우
source
에 요소가 없는 경우
예제
다음 코드 예제에서는 을 사용하여 Aggregate문자열의 단어 순서를 되돌리는 방법을 보여 줍니다.
string sentence = "the quick brown fox jumps over the lazy dog";
// Split the string into individual words.
string[] words = sentence.Split(' ');
// Prepend each word to the beginning of the
// new sentence to reverse the word order.
string reversed = words.Aggregate((workingSentence, next) =>
next + " " + workingSentence);
Console.WriteLine(reversed);
// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the
Sub AggregateEx1()
Dim sentence As String =
"the quick brown fox jumps over the lazy dog"
' Split the string into individual words.
Dim words() As String = sentence.Split(" "c)
' Prepend each word to the beginning of the new sentence to reverse the word order.
Dim reversed As String =
words.Aggregate(Function(ByVal current, ByVal word) word & " " & current)
' Display the output.
Console.WriteLine(reversed)
End Sub
' This code produces the following output:
'
' dog lazy the over jumps fox brown quick the
설명
이 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 메서드를 사용하면 값 시퀀스에 대해 계산을 간단하게 수행할 수 있습니다. 이 메서드는 첫 번째 요소를 제외한 각 요소에 source
대해 한 번 호출 func
하여 작동합니다. 호출 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 될 때마다 func
시퀀스의 요소와 집계된 값(첫 번째 인수로)을 func
모두 전달합니다. 첫 번째 요소는 source
초기 집계 값으로 사용됩니다. 결과는 func
이전 집계된 값을 대체합니다. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 의 최종 결과를 반환합니다 func
.
메서드의 Aggregate 이 오버로드는 첫 번째 요소를 source
초기 집계 값으로 사용하기 때문에 모든 경우에 적합하지 않습니다. 반환 값에 특정 조건을 충족하는 요소 source
만 포함해야 하는 경우 다른 오버로드를 선택해야 합니다. 예를 들어 짝 source
수의 합계를 계산하려는 경우 이 오버로드는 신뢰할 수 없습니다. 첫 번째 요소가 짝수 대신 홀수이면 결과가 올바르지 않습니다.
일반적인 집계 작업을 간소화하기 위해 표준 쿼리 연산자는 범용 개수 메서드Count와 4개의 숫자 집계 메서드, 즉 Min, MaxSum및 Average.