Automapper Enumerable - C#
Формулировка задачи:
Привет всем!
Не могли бы вы объяснить как правильно мапить классы с List внутри?
Как правильно писать ForMember для таких случаев?
static void Main(string[] args)
{
itemDTO item = new itemDTO();
item.Id = "10";
item.Name = "qwe";
item.Subs = new List<SubitemDTO>() {
new SubitemDTO() { Id = 10, Name= "q1", Description="a1" },
new SubitemDTO() { Id = 10, Name= "q2", Description="a2" },
new SubitemDTO() { Id = 10, Name= "q3", Description="a3" }
};
Mapper.Initialize(cfg => cfg.CreateMap<itemDTO, itemModel>()
.ForMember(a=>a.Subs, opt => opt.MapFrom(c => c.Subs)));
var y = Mapper.Map<itemDTO, itemModel>(item);
Console.ReadLine();Решение задачи: «Automapper Enumerable»
textual
Листинг программы
itemDTO item = new itemDTO
{
Id = "10",
Name = "qwe",
Subs = new List<SubitemDTO>()
{
new SubitemDTO() {Id = 10, Name = "q1", Description = "a1"},
new SubitemDTO() {Id = 10, Name = "q2", Description = "a2"},
new SubitemDTO() {Id = 10, Name = "q3", Description = "a3"}
}
};
var config = new MapperConfiguration(c =>
{
c.CreateMap<itemDTO, itemModel>();
c.CreateMap<SubitemDTO, SubItemModelView>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var dest = mapper.Map<itemDTO,itemModel>(item);