I'm using AutoMapper and one line causes an error when I try to assign null in ternary operator like this:
.ForMember(dest => dest.VatCategoryTaxAmount,
opt => opt.MapFrom(src => src.TaxSubtotal != null &&
src.TaxSubtotal.TaxCategory != null
? double.Parse(src.TaxSubtotal.TaxCategory.Percent)
: null));
Error:
Cannot convert anonymous method block to type 'type' because it is not a delegate type
When I use 0
instead of null
, then it works.
I'm confused because the destination property is of type double?
. I want to assign to it a null value.
How can I do that?
The problem is your conditional assignment doesn't return the same type.
src.TaxSubtotal.TaxCategory != null ? double.Parse(src.TaxSubtotal.TaxCategory.Percent) : null
it returns a double
if true or null
if false. Try to cast the true assignment to a nullable.