使用 Automapper 使用来自源的 属性 值从对象列表映射到单个对象

Use Automapper to map from a list of objects to a single object using property values from source

来源:

public class Message 
{
    public DateTime AcceptedDate { get; set; }
    public List<PriceDetail> PriceDetails { get; set; }
}

public class PriceDetail 
{
    public string ServiceCode { get; set; }
    public string ServiceValue { get; set; }
}

目的地:

public class GroupEntity 
{
    public DateTime AcceptedDate { get; set; }
    public List<PlanEntity> Plans { get; set; }
}

public class PlanEntity 
{
    public string MetalLevel { get; set; }
    public string MdCode { get; set; }
    public string RxCode { get; set; }
    public string PercentChange { get; set; }
}

Source PriceDetail 可能类似于:

我需要将 PriceDetail 映射到 PlanEntity,例如:

我知道映射器中会有硬编码逻辑,但不确定如何或是否可以使用自动映射器。非常感谢任何提示或建议。

CreateMap<PriceDetail, PlanEntity>()
           .ForMember(dest => dest.MetalLevel, opt => {
               opt.PreCondition(src => src.ServiceCode=="MetalLevel");
               opt.MapFrom(src => src.ServiceCode);
           });
CreateMap<PriceDetail, PlanEntity>()
           .ForMember(dest => dest.RxCode, opt => {
               opt.PreCondition(src => src.ServiceCode=="RxCode");
               opt.MapFrom(src => src.ServiceCode);
           });