Automapper: map properties manually

前端 未结 3 1245
轻奢々
轻奢々 2021-02-07 07:11

I just started to use automapper to map DTOs<->Entities and it seems to be working great.

In some special cases I want to map only some properties and perform additio

3条回答
  •  萌比男神i
    2021-02-07 07:40

    If I read your question correctly, yes, as long as your destination (target) property matches your conversion.

    So if I am going from a string to a bool for a Status of "A" or "I" (active/inactive), I can do something like:

    .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status == "A"))
    

    And then when going the other direction, convert it back:

    .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status ? "A" : "I"))
    

    A date example:

    .ForMember(dest => dest.SomeDate, opt => opt.MapFrom(src => src.SomeDate.ToString("M/d/yyyy")));
    

提交回复
热议问题