Cannot solve Sum method

Tried to reuse Sum, but got this error:

can't solve Sum method

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);
        }
+5
source share
1 answer

LINQ does not provide a standard default version Sumdue to the fact that the language and runtime (before dynamic) do not support allowing common ( <T>) to be added; there is no interface INumber, and the language does not support operators ( +) for generic types ( <T>). Go to overloads that accept Func<T, float>, Func<T, int>etc. - or use the MiscUtil library in whichincludes general Sum<T>, in particular:

public static TSource Sum<TSource>(this IEnumerable<TSource> source);
public static TValue Sum<TSource, TValue>(this IEnumerable<TSource> source,
                        Func<TSource,TValue> selector);

, + ( ..).

+6

All Articles