linq Aggregate

linq Aggregate

public static void Aggregate()
        {
            // Aggregate - Simple
            double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

            double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);

            Console.WriteLine("Total product of all numbers: {0}", product);

            // Aggregate - Seed
            double startBalance = 100.0;

            int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };

            double endBalance = attemptedWithdrawals.Aggregate(startBalance,
                                (balance, nextWithdrawal) =>((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

            Console.WriteLine("Ending balance: {0}", endBalance);
        }
Output

linq Aggregate Operators

linq Aggregate linq Aggregate Reviewed by Bhaumik Patel on 6:59 PM Rating: 5