Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't assign null value to a double? property in AutoMapper

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading