bobuva
bobuva

Reputation: 183

LINQ to SQL sum of decimal values

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

Upvotes: 2

Views: 15387

Answers (2)

itowlson
itowlson

Reputation: 74802

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).

Upvotes: 6

Andrew Hare
Andrew Hare

Reputation: 351456

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>

Upvotes: 4

Related Questions