我有一个对象AllDriversDetails包含两个驱动程序的数据:第一个和第二个。
public class AllDriversDetails
{
public string FirstDriverId { get; set; }
public string FirstDriverName { get; set; }
public string SecondDriverId { get; set; }
public string SecondDriverName { get; set; }
}
我必须将AllDriversDetails转换为具有两个相同类型属性的ComplexDriversDetails
public class ComplexDriversDetails
{
public DriverDetails FirstDriver { get; set; }
public DriverDetails SecondDriver { get; set; }
}
public class DriverDetails
{
public string Id { get; set; }
public string Name { get; set; }
}
是否可以使用automapper?
答案 0 :(得分:0)
我应该没问题。试试这样的事情
Mapper.CreateMap<AllDriversDetails, ComplexDriversDetails>()
.ForMember(m => m.FirstDriver, opt => opt.MapFrom(src => new DriverDetails {Id = src.FirstDriverId, Name = FirstDriverName }))
.ForMember(m => m.SecondDriver , opt => opt.MapFrom(src => new DriverDetails {Id = src.SecondDriverId, Name = SecondDriverName }))
您可以在此处找到更多信息:http://cpratt.co/using-automapper-creating-mappings/