I am trying to use SUM()
to total all the entries in a column and then convert the number to an int
.
I am running into a problem in the conversion because some of the entries are NULL. I tried to exclude them using a != null
in a WHERE()
clause but I am still getting the same error
The cast to value type
Int32
failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
Here is my LINQ query, can someone point out what I am missing in order to properly exclude nulls?
TotalLiftings = db.Query<Transaction>().
Where(tr => tr.TerminalId == TerminalUserData.ID &&
tr.ProductId == t.ProductId &&
tr.TransactionDate == t.InventoryDate &&
tr.NetGallons != null).
Select(tr => tr.NetGallons).
Sum();