I have a simple linq query
(From a In db.payments Group Join b In db.interestcharges On a.pid Equals b.pid Into totalinterest = Group, TotalInterestReceived = Sum(b.interest)) select a, TotalInterestReceived).toList()
b.interest
is type decimal in DB.
Throws
"The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."
This is because InterestCharges
table may not have any interest records for pid
.
I have tried sum(if(b.interest=nothing,0,b.interest)
but this is translated by LINQ to if(b.interest=0, 0, b.interest)
hence it never checks for null. Nothing else seems to work instead of nothing. I have tried vbNull
, isDBNull()
, no success. query works fine when the sum is not null. defaultifempty(0)
may work but not sure how to use it in this scenario. Any pointers?