When using QueryableExtensions you have to be explicit with some type conversions, such as int?
to int
. This is probably the source of the "Argument types do not match" exception.
If you have many properties that need a type conversion -- like if you had many other properties where you find you are doing c.MyVariable ?? 0
-- you can instead define a conversion rule and not have to be explicit about every property.
Normally, to do type conversions in Automapper, you would use ConvertUsing
but when using QueryableExtensions, you need to instead use ProjectUsing
.
You could use the following line and it will take care of all mappings from int?
to int
without the need to explicitly specify the mappings for each property:
cfg.CreateMap<int?, int>().ProjectUsing(src => src.HasValue ? src.Value : 0);