The problem is NOT that the Price data type is Nullable. In fact, the problem is that it is NOT nullified and there is an empty set. The EF function Sum()can only work with empty sets if it deals with nullable data. I know this does not make sense, because empty sets and types that allow NULL values โโare not the same thing. Just cast it to a nullable type and you will get a nullable answer. In the case of an empty set, the answer will be zero. So this is the only case that will work.
Int64? amount = db.Items.Where(x => x.ItemOrdered == true).Sum(x => (Int64?) x.Price);
source
share