我有一个类名为Foos:
public class Foos
{
public string TypeName;
public IEnumerable<int> IDs;
}
能否使用AutoMapper将它映射为Foo对象的IList?
public class Foo
{
public string TypeName;
public int ID;
}
我有一个类名为Foos:
public class Foos
{
public string TypeName;
public IEnumerable<int> IDs;
}
能否使用AutoMapper将它映射为Foo对象的IList?
public class Foo
{
public string TypeName;
public int ID;
}
Omu的回答给了我一个解决问题的思路(对于这个建议+1)。我使用了ConstructUsing()方法,它对我有用:
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;
}
}