Tuesday, June 23, 2015

IEnumerable Sum extension for complex type

I was not able to find a simple/working example in internet. This is how to do it:
public static Money Sum<T>(this IEnumerable<T> data, Func<T, Money> selector)
        {
            var money = new Money(0);
            money = data.Select(selector.Invoke).Aggregate(money, (current, selectedMoney) => current + selectedMoney);
            return money;
        }

No comments: