From the following simulation
int[] amountWithdrawal = { 10, 20, 30, 140, 50, 70 };
amountWithdrawal.Aggregate(100, (balance, withdrawal) =>
{
Console.WriteLine("balance :{0},Withdrawal:{1}", balance, withdrawal);
if (balance >= withdrawal)
{
return balance - withdrawal;
}
else return balance;
}
);
I want to terminate the Aggregation when the balance is less than the withdrawal.But my code travels the entire array.How to terminate it?
It seems to me that you want an
Accumulatemethod which yields a new sequence of accumulated values, instead of a scalar. Something like this:Then you could apply
TakeWhile:I could have sworn there was something like this in normal LINQ to Objects, but I can’t find it at the moment…