Reputation: 236
Starting from these example:
public class A
{
public int[] ArrayOfIds { get; set; }
}
public class B
{
public List<C> MyList { get; set; }
}
public class C
{
public int Id { get; set; }
}
I want to create a mapping from A to B, where B contains a list of C objects, identified with ids of A.
How can I configure AutoMapper to achieve this?
Upvotes: 3
Views: 1819
Reputation: 7706
You can do the following:
1. Create Map from int
to C
, so that you can do casting on int[]
2. Create Map from A
to B
A a = new A();
a.ArrayOfIds = new int[] { 1, 2, 3 };
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<int, C>().ForMember(dest => dest.Id, opts => opts.MapFrom(src => src));
cfg.CreateMap<A, B>().ForMember(dest => dest.MyList, opts => opts.MapFrom(src => src.ArrayOfIds));
});
IMapper mapper = config.CreateMapper();
var b = mapper.Map<B>(a);
Upvotes: 5