My code is here:
Int64? amount = db.Items.Where(x => x.ItemOrdered == true).Sum(x => x.Price);
That work fine but through Error database is empty
The cast to value type
'System.Int32'
failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
I want replace it with 0 (zero)
I am using entity frame work with MVC application
For anyone having issues because Price is not nullable but the where clause is returning an empty set you can just cast price to be nullable but then move the null coalescing check outside:
Int64 amount = db.Items.Where(x => x.ItemOrdered == true).Sum(x => (Int64?) x.Price) ?? 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With