You get cast exceptions in one case and no exceptions in the other because Convert.ToDecimal
is a lot more lenient than a simple cast. Cast exception indicates that the underlying field is not a decimal
, but it has a type that can be easily converted to decimal
.
To use Convert.ToDecimal
inside LINQ query, change your code as follows:
SaleDiscount = (
Convert.ToDecimal(i.Field<object>("OnSalePercentAdjustment")) *
Convert.ToDecimal(i.Field<object>("Price"))),
Alternatively, you could figure out the correct type, use Field<correct-type>(...)
to read it, and then do the cast to decimal
.