Tried to reuse Sum but got this error:
cannot resolve method Sum
how can I change my syntax
private static void AggregatesData(User user)
{
user.TotalActiveUsers = SumToolbarsData(user.bars,(tb => tb.ActiveUsers));
user.TotalInstalls = SumToolbarsData(user.bars, (tb => tb.Installs));
user.TotalBalance = SumToolbarsData(user.bars, (tb => tb.Balance));
}
private static T SumToolbarsData<T>(List<Bar> bars, Func<Bar, T> selector)
{
return bars.Sum<T>(selector);
}
LINQ does not provide, out of the box, a generic version of
Sum, for the reason that the language and runtime (prior todynamic) does not allow generic (<T>) values to be added; there is noINumberinterface, and the language does not support operators (+) on generic types (<T>). Either switch to overloads that takeFunc<T, float>,Func<T, int>, etc – or use the MiscUtil library which does include a genericSum<T>implementation, specifically:which it does by resolving the
+operator, and using some smoke and mirrors (so that it isn’t slow, etc).