我有一堂课Foos:
public class Foos
{
public string TypeName;
public IEnumerable<int> IDs;
}
是否可以将它与AutoMapper映射到ILoo的Foo对象?
public class Foo
{
public string TypeName;
public int ID;
}
答案 0 :(得分:4)
private class MyProfile : Profile
{
protected override void Configure()
{
CreateMap<Foos, Foo>()
.ForMember(dest => dest.ID, opt => opt.Ignore());
CreateMap<Foos, IList<Foo>>()
.ConstructUsing(x => x.IDs.Select(y => CreateFoo(x, y)).ToList());
}
private Foo CreateFoo(Foos foos, int id)
{
var foo = Mapper.Map<Foos, Foo>(foos);
foo.ID = id;
return foo;
}
}