0

I am trying to convert from bool value to decimal i automapper. It works fine if i dont check for null values .But i want to check for null values and if the value is null ,let the value be null i destination else convert to decimal.Below is the ode i tied but i am getting a error.

  cfg.CreateMap<sourcemodel, destinatiomodel>()    
     .ForMember(dest =>  dest.WorkhoursPerWeek != null ? 
                Convert.ToDecimal(dest.WorkhoursPerWeek) : null, 
                opts => opts.MapFrom(src => src.cstu_WorkHoursPerWeek));
Fals
  • 6,813
  • 4
  • 23
  • 43
user2465036
  • 351
  • 2
  • 11
  • 24

1 Answers1

0

You code doesn't make sense (mainly because it's invalid). If you need to check the source property first, I'd suggest using AfterMap()

cfg.CreateMap<sourcemodel, destinatiomodel>()    
  .AfterMap((src, dest) =>  
    {
      dest = dest.WorkhoursPerWeek != null 
      ? Convert.ToDecimal(dest.WorkhoursPerWeek) 
      : src.cstu_WorkHoursPerWeek
    });

(Or this might not work, and if not, use BeforeMap() to map before and Ignore() the property).

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • I highly recommend when you communicate with other technical people (anywhere not just SO) that you be much more descriptive about a problem you face. For example, what can I possible do with a `I am getting an error`? I don't have an exception, nor code; you haven't presented me with any information to help you. – Erik Philips Aug 18 '16 at 16:14
  • cfg.CreateMap() .AfterMap((src, dest) => { dest = dest.Score != null ? Convert.ToDecimal(dest.Score) : src.cstu_Score }) .ForMember(dest => dest.ApplicantExamId, opts => opts.MapFrom(src => src.cstu_ApplicantExamId)) .ReverseMap(); – user2465036 Aug 18 '16 at 16:21
  • Cannot simply convert type decimal? to Model – user2465036 Aug 18 '16 at 16:21
  • probably need to change `dest =` to `dest.property =`. – Erik Philips Aug 18 '16 at 16:40
  • @Erik..Did not work.Can you please test the code and send if you have time or else it is fine – user2465036 Aug 18 '16 at 18:22