I have a class Foos:
public class Foos
{
public string TypeName;
public IEnumerable<int> IDs;
}
Is it possible to map it with AutoMapper to IList of Foo objects?
public class Foo
{
public string TypeName;
public int ID;
}
I have a class Foos:
public class Foos
{
public string TypeName;
public IEnumerable<int> IDs;
}
Is it possible to map it with AutoMapper to IList of Foo objects?
public class Foo
{
public string TypeName;
public int ID;
}
here's the solution with the ValueInjecter
var list = new List<Foo>();
foos.IDs.AsParallel().ForAll(id =>
{
var foo = new Foo();
foo.InjectFrom(foos);
foo.ID = id;
list.Add(foo);
});
Omu's answer gaved me an idea how to solve the problem (+1 for suggestion). I've used ConstructUsing() method and it worked for me:
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;
}
}