C# – The null value cannot be assigned to a member with type decimal which is a non-nullable value type

cexceptionlinq-to-sqlnet

An exception occurs when the following IQueryable is enumerated:

from record in dataContext.SomeTable
select Convert.ToDecimal(record.nullableDecimalColumn);

The error is an InvalidOperationException:

The null value cannot be assigned to a member with type decimal which is a non-nullable value type

The documentation for Convert.ToDecimal says that it converts null to 0, so it looks like it should work correctly.

Best Answer

If you wrote Query like this

tot_vat_amt = _pd.Where(x => x.VAT == 4m).Sum(x =>x.amount)

Replace with

tot_vat_amt = _pd.Where(x => x.VAT == 4m).Sum(x =>(double?)x.amount)

Hope this will help you