Enumerable.Aggregate 메서드

정의

오버로드

Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence. 지정된 시드 값은 초기 누적기 값으로 사용되고 지정된 함수는 결과 값을 선택하는 데 사용됩니다.The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence. 지정된 시드 값은 초기 누적기 값으로 사용됩니다.The specified seed value is used as the initial accumulator value.

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence.

Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence. 지정된 시드 값은 초기 누적기 값으로 사용되고 지정된 함수는 결과 값을 선택하는 데 사용됩니다.The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

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 요소의 형식입니다.The type of the elements of source.

TAccumulate

누적기 값의 형식입니다.The type of the accumulator value.

TResult

결과 값의 형식입니다.The type of the resulting value.

매개 변수

source
IEnumerable<TSource>

집계할 IEnumerable<T>입니다.An IEnumerable<T> to aggregate over.

seed
TAccumulate

초기 누적기 값입니다.The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

각 요소에 대해 호출할 누적기 함수입니다.An accumulator function to be invoked on each element.

resultSelector
Func<TAccumulate,TResult>

최종 누적기 값을 결과 값으로 변환하는 함수입니다.A function to transform the final accumulator value into the result value.

반환

TResult

변환된 최종 누적기 값입니다.The transformed final accumulator value.

예외

source, func 또는 resultSelectornull인 경우source or func or resultSelector is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. Aggregate 누적기 함수 및 결과 선택기를 적용 합니다.The following code example demonstrates how to use Aggregate to apply an accumulator function and a result selector.

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.
    MsgBox("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>) 메서드를 사용 하면 간단 하 게 값의 시퀀스에 대해 계산을 수행 합니다.The Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) method makes it simple to perform a calculation over a sequence of values. 이 메서드를 호출 하 여 작동 func 의 각 요소 마다 한 번씩 source합니다.This method works by calling func one time for each element in source. 매번 func 가 호출 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 두 요소의 시퀀스에서 집계 된 값을 전달 (첫 번째 인수로 func).Each time func is called, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) passes both the element from the sequence and an aggregated value (as the first argument to func). 값을 seed 초기 집계 값으로 매개 변수를 사용 합니다.The value of the seed parameter is used as the initial aggregate value. 결과 func 이전 집계 값을 대체 합니다.The result of func replaces the previous aggregated value. 최종 결과 func 넘어갑니다 resultSelector 의 최종 결과 얻으려면 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)합니다.The final result of func is passed to resultSelector to obtain the final result of Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>).

일반 집계 작업을 간소화 하려면 표준 쿼리 연산자 포함 범용 수 메서드, Count, 및 4 개의 숫자 집계 메서드를 namely MinMax, Sum, 및 Average합니다.To simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence. 지정된 시드 값은 초기 누적기 값으로 사용됩니다.The specified seed value is used as the initial accumulator value.

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 요소의 형식입니다.The type of the elements of source.

TAccumulate

누적기 값의 형식입니다.The type of the accumulator value.

매개 변수

source
IEnumerable<TSource>

집계할 IEnumerable<T>입니다.An IEnumerable<T> to aggregate over.

seed
TAccumulate

초기 누적기 값입니다.The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

각 요소에 대해 호출할 누적기 함수입니다.An accumulator function to be invoked on each element.

반환

TAccumulate

최종 누적기 값입니다.The final accumulator value.

예외

source 또는 funcnull인 경우source or func is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. Aggregate 누적기 함수를 적용 하 여 시드 값을 사용 합니다.The following code example demonstrates how to use Aggregate to apply an accumulator function and use a seed value.

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.
    MsgBox("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>) 메서드를 사용 하면 간단 하 게 값의 시퀀스에 대해 계산을 수행 합니다.The Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) method makes it simple to perform a calculation over a sequence of values. 이 메서드를 호출 하 여 작동 func 의 각 요소 마다 한 번씩 source합니다.This method works by calling func one time for each element in source. 매번 func 가 호출 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 두 요소의 시퀀스에서 집계 된 값을 전달 (첫 번째 인수로 func).Each time func is called, Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) passes both the element from the sequence and an aggregated value (as the first argument to func). 값을 seed 초기 집계 값으로 매개 변수를 사용 합니다.The value of the seed parameter is used as the initial aggregate value. 결과 func 이전 집계 값을 대체 합니다.The result of func replaces the previous aggregated value. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 최종 결과 반환 합니다 func합니다.Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) returns the final result of func.

일반 집계 작업을 간소화 하려면 표준 쿼리 연산자 포함 범용 수 메서드, Count, 및 4 개의 숫자 집계 메서드를 namely MinMax, Sum, 및 Average합니다.To simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

시퀀스에 누적기 함수를 적용합니다.Applies an accumulator function over a sequence.

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 요소의 형식입니다.The type of the elements of source.

매개 변수

source
IEnumerable<TSource>

집계할 IEnumerable<T>입니다.An IEnumerable<T> to aggregate over.

func
Func<TSource,TSource,TSource>

각 요소에 대해 호출할 누적기 함수입니다.An accumulator function to be invoked on each element.

반환

TSource

최종 누적기 값입니다.The final accumulator value.

예외

source 또는 funcnull인 경우source or func is null.

source에 요소가 없는 경우source contains no elements.

예제

다음 코드 예제를 사용 하 여 문자열에서 단어의 순서를 반대로 하는 방법에 설명 Aggregate합니다.The following code example demonstrates how to reverse the order of words in a string by using 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.
    MsgBox(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>) 메서드를 사용 하면 간단 하 게 값의 시퀀스에 대해 계산을 수행 합니다.The Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) method makes it simple to perform a calculation over a sequence of values. 이 메서드를 호출 하 여 작동 func 의 각 요소 마다 한 번씩 source 첫 번째 것을 제외 하 고 있습니다.This method works by calling func one time for each element in source except the first one. 매번 func 가 호출 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 두 요소의 시퀀스에서 집계 된 값을 전달 (첫 번째 인수로 func).Each time func is called, Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) passes both the element from the sequence and an aggregated value (as the first argument to func). 첫 번째 요소 source 초기 집계 값으로 사용 됩니다.The first element of source is used as the initial aggregate value. 결과 func 이전 집계 값을 대체 합니다.The result of func replaces the previous aggregated value. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 최종 결과 반환 합니다 func합니다.Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns the final result of func.

이 오버 로드는 Aggregate 의 첫 번째 요소를 사용 하기 때문에 메서드는 모든 경우에 적합 한 없습니다 source 초기 집계 값으로.This overload of the Aggregate method isn't suitable for all cases because it uses the first element of source as the initial aggregate value. 반환 값의 요소를 포함 해야 하는 경우에 다른 오버 로드를 선택 해야 source 특정 조건을 충족 하는 합니다.You should choose another overload if the return value should include only the elements of source that meet a certain condition. 이 오버 로드 되지에서 짝수의 합계를 계산 하려는 경우에 신뢰할 수 있는 예를 들어 source합니다.For example, this overload isn't reliable if you want to calculate the sum of the even numbers in source. 결과 첫 번째 요소 대신도 홀수 이면 올바르지 않을 수 있습니다.The result will be incorrect if the first element is odd instead of even.

일반 집계 작업을 간소화 하려면 표준 쿼리 연산자 포함 범용 수 메서드, Count, 및 4 개의 숫자 집계 메서드를 namely MinMax, Sum, 및 Average합니다.To simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

적용 대상