views:

163

answers:

2

I can't figure out how to do a simple sum of decimal values.

Table<StaffTime> times = ctx.GetTable<StaffTime>();

var query = from t in times
            select new 
            {
               t.Hours.Sum()
            }

Isn't Sum an extension method? What am I missing?

Bob

+3  A: 

Try this:

var sum = (from t in times select t.Hours).Sum();

This is of course assuming that t.Hours is one of these types:

Decimal
Double
Int32
Int64
Nullable<Decimal>
Nullable<Double>
Nullable<Int32>
Nullable<Int64>
Nullable<Single>

Andrew Hare
Andrew,I'm creating an anonymous type using select new, as below:var query = from t in times select new { t.Hours.Sum(), t.StaffID, ...};I'm not sure I can use the Sum extension method in this case.
bobuva
You are correct - the `Sum` method wouldn't work for your case then.
Andrew Hare
+4  A: 

Sum is an extension method over IEnumerable<decimal> (or int or whatever). t.Hours is a single decimal value (I assume), so can't be summed.

If you want the sum of hours, write times.Sum(t => t.Hours).

itowlson