I have a sequence of numbers:
var seq = new List { 1, 3, 12, 19, 33 };
and I want to transform that into a new sequence where
To use Linq and only iterate over the list once you could use a custom aggregator:
class Aggregator
{
public List List { get; set; }
public int Sum { get; set; }
}
..
var seq = new List { 1, 3, 12, 19, 33 };
var aggregator = new Aggregator{ List = new List(), Sum = 0 };
var aggregatorResult = seq.Aggregate(aggregator, (a, number) => { a.Sum += number; a.List.Add(a.Sum); return a; });
var result = aggregatorResult.List;