简体   繁体   English

Automapper 将多个列表从源映射到目标上的单个列表

[英]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.还有一个棘手的部分是,在 Quote 费用上,有一个 Type 字段需要根据它从哪个列表映射来设置。 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.因此,即如果它们是 CarrierCharges,则需要将类型设置为 Carrier 等。我正在使用 AfterMap 执行此操作,但我想问问是否有更好的方法。

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>()
   ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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