Reputation: 587
I have an object (ProductModel) that has a nested list of images. I am trying to simplify the model (Product) that has this list as its property. I am using Automapper, but I can not seem to get the mapping configuration right. I viewed several other posts, but they seem to be a little different than what I am trying to achieve.
// Map to:
public class Product
{
public List<Image> Images { get; set; }
}
public class Image
{
public string url { get; set; }
}
// Map from:
public class ProductModel
{
public ImageSet ImageSet { get; set; }
}
public class ImageSet
{
public List<ImageDetail> ImageDetails { get; set; }
}
public class ImageDetail
{
public string Url { get; set; }
}
Upvotes: 2
Views: 1923
Reputation: 30618
The following configuration should work:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ImageDetail, Image>();
cfg.CreateMap<ProductModel, Product>()
.ForMember(dest => dest.Images, opt => opt.MapFrom(src => src.ImageSet.ImageDetails))
;
});
Upvotes: 2