When a this function is given an id that in SQL returns 0 rows I get the error below:
The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.
What is the best way to modify the query to prevent this. I am pulling the value from a different table than I am querying here, so I would like to be able to handle giving it a “bad” id.
public decimal GetSumOfValuesForAccount(Guid companyId)
{
return (from o in _context.Details
where o.CustomerID == companyId
select o).Sum(p => p.SomeValue.GetValueOrDefault());
}
Returns the error:
you should be able to either return:
or
The
??operator will check for null, and if null, return the next value.