Map a property to a collection item

后端 未结 1 562
不思量自难忘°
不思量自难忘° 2020-12-14 09:01

I\'ve been sifting through AutoMapper documentation to try and find a recommended solution to this but haven\'t been able to find it.

Let\'s say I have a class like

相关标签:
1条回答
  • 2020-12-14 10:01

    I'm thinking something like this should work (not tested -- just typing out loud):

    Mapper.CreateMap<Foo, Bar>().ForMember(d => d.Notes,
        opt => opt.MapFrom(s => new List<Note> { new Note { Text = s.Note } });
    

    EDIT

    You could also use AutoMappers AfterMap functionality. This lambda would be executed after Automapper has done it's regular mappings:

    .AfterMap((s,d) => d.Notes.Add(new Note { Text = s.Note }));
    
    0 讨论(0)
提交回复
热议问题