score:1

Accepted answer

using Automapper version 5.2.0, your syntax should be as below. You should use ResolveUsing instead of MapFrom and swith-case should be within curly brackets. you dont have to use any Mapper.Initialize(). I tested this code and it works for me. I hope that it helps for you also. Cheers.

Similar question asked here. see the answer from Mrchief

   var config = new MapperConfiguration(cfg =>
                    {
        Mapper.CreateMap<Report, ReportViewModel>()
            .ForMember(src => src.Value, 
                       dest => dest.ResolveUsing(r => {
                            switch(reportingPeriod)
                            {
                                case ReportingPeriod.Daily:
                                    return r.Day.Value;
                                    break;
                                case ReportingPeriod.Weekly:
                                    return r.Week.Value;
                                    break;
                                case ReportingPeriod.Monthly:
                                    return r.Month.Value;
                                    break;
                                case default:
                                    //None
                                    return null;
                                    break;
                               }
                            }));
            });

       var mapper = config.CreateMapper();

score:0

You need to return the value. IMO a switch statement is clearer than nested if else:

Mapper.CreateMap<Report, ReportViewModel>()
    .ForMember(src => src.Value, 
               dest => dest.MapFrom(r => 
                    switch(reportingPeriod)
                    {
                        case ReportingPeriod.Daily:
                            return r.Day.Value;
                            break;
                        case ReportingPeriod.Weekly:
                            return r.Week.Value;
                            break;
                        case ReportingPeriod.Monthly:
                            return r.Month.Value;
                            break;
                        case default:
                            //None
                            return null;
                            break;
                    }));

Related Query

More Query from same tag