![](/img/trans.png)
[英]AutoMapper - How to map entire object from source's single property?
[英]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; }
}
源 PriceDetail 可能类似于:
我需要 map PriceDetail 到 PlanEntity,例如:
我知道映射器中会有硬编码逻辑,但不确定如何或是否可以使用 automapper。 非常感谢任何提示或建议。
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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.