views:

29

answers:

2

I'm trying to get the Sum() from an Entityset<MyObject> with the next query.

(from MyObject p in SelectedObject.MyObjectEntitySet
where p.AColumn.HasValue && 
p.ADate >= dateTimeValue &&
p.ADate <= dateTimeValue2
select p.AColumn.Value).Sum();

with no luck retrieving correct sum.

Any Ideas?

[EDIT] OK WORKED!

A: 

You can troubleshoot the query by breaking it up into its constituent parts, and examining the output of each part:

var entitySet = SelectedObject.MyObjectEntitySet;
var entitySetWithValues = entitySet.Where(p => p.AColumn.HasValue);
var entitySetGreater = entitySetWithValues.Where(p => p.ADate >= DateTimeValue);

...and so forth.

Robert Harvey
A: 

If you see the list of overloads, the Sum method is over numeric values (decimal, float, int, double, long, etc). The selector has to be a function that takes a DateTime and returns a numeric value.

[edit] Sorry, I didn't realize that AColumn was numeric.

uvita