我有一个CarDto
和Car
域对象。
汽车有司机清单。
public class Car
{
public int Id {get; set; }
public int Name {get; set; }
public ICollection<Driver> Drivers {get; set; }
}
public class CarVM
{
public int Id {get; set; }
public int Name {get; set; }
public string DriverBadge {get; set; }
public string[] Drivers {get; set; }
}
我正在尝试使用AutoMapper映射此对象
CreateMap<CarVM, Car>()
.ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name))
.ForMember(dest => dest.Drivers, opts => {
... how to map here?
})
答案 0 :(得分:1)
您要做的就是将string[]
映射到List<>
。只需使用以下
CreateMap<CarVM, Car>()
.ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name))
.ForMember(dest => dest.Drivers, opts => opts.MapFrom(s=> s.Drivers.ToList()))
});