简体   繁体   中英

Automapper mapping multiple lists from source to single list on destination

What I have is this:

public class Rate
{
    public List<Charge> CarrierCharges { get; set; }
    public List<Charge> CustomerCharges { get; set; }
    public List<Charge> ThirdPartyCharges { get; set; }
}

And I need it mapped to this:

public class Quote
{
    public List<Charge> Charges { get; set; }        
}

What needs to happen is it needs to add to the list of charges on the Quote. Also the tricky part is that on the Quote charge, there is a Type field that needs to be set based upon which list it was mapped from. So ie if they are CarrierCharges then the type needs to be set to Carrier, etc. I'm doing this with AfterMap but figured I'd ask to see if there was a better way.

You want to do the following:

  Mapper.CreateMap<Rate, Quote>()
   .ForMember(t => t.Charges, o => o.MapFrom(s => s.CarrierCharges
      .Concat(s.CustomerCharges)
      .Concat(s.ThirdPartyCharges)
      .MapList<QuoteNamespace.Charge>()
   ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM