Automapper: complex if else statement in ForMember

Use ResolveUsing method:

Mapper.CreateMap<SomeViewModels, SomeDTO>()               
         .ForMember(dest => dest.Date, o => o.ResolveUsing(Converter));

private static object Converter(SomeViewModels value)
{
    DateTime? finalDate = null;
    if (value.Date.HasDate == "N")
    {
        // so it should be null
    }
    else
    {                                   
        finalDate = DateTime.Parse(value.Date.ToString());
    }                               
    return finalDate;
}

Here is more information: AutoMapper: MapFrom vs. ResolveUsing


In recent versions of AutoMapper, ResolveUsing was removed. Instead, use a new overload of MapFrom:

void MapFrom<TResult>(Func<TSource, TDestination, TResult> mappingFunction);

Just adding another lambda/function parameter will dispatch to this new overload:

        CreateMap<TSource, TDest>()
                .ForMember(dest => dest.SomeDestProp, opt => opt.MapFrom((src, dest) =>
                {
                    TSomeDestProp destinationValue;

                    // mapping logic goes here

                    return destinationValue;
                }));