Aggregate C# with Example
Aggregate Applies an accumulator function over a sequence. int[] intList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = intList.Aggregate((prevSum, current) => prevSum + current); // sum = 55 At the first step prevSum = 1 At the second prevSum = prevSum(at the first step) + 2 At the i-th step prevSum = prevSum(at the (i-1) step) + i-th element of the array string[] stringList = { "Hello", "World", "!" }; string joinedString = stringList.Aggregate((prev, current) => prev + " " + current); // joinedString = "Hello World !" A second overload of Aggregate also receives an seed parameter which is the initial accumulator value. This can be used to calculate multiple conditions on a collection without iterating it more than once. List items = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; For the collection of items we want to calculate 1. The total .Count 2. The amount of even numbers 3. Collect each forth item Using Aggregate it can be done like this: var result = items.Aggregate(new { Total = 0, Even = 0, FourthItems = new List() }, (accumelative,item) => new { Total = accumelative.Total + 1, Even = accumelative.Even + (item % 2 == 0 ? 1 : 0), FourthItems = (accumelative.Total + 1)%4 == 0 ? new List(accumelative.FourthItems) { item } : accumelative.FourthItems }); // Result: // Total = 12 // Even = 6 // FourthItems = [4, 8, 12] Note that using an anonymous type as the seed one has to instantiate a new object each item because the properties are read only. Using a custom class one can simply assign the information and no new is needed (only when giving the initial seed parameter