I have a problem with Automapper. I set up a test windows form application and below is the code. Also look at the comments after each MessageBox:
public cla
I asked a similar question here and found another similar one here.
I think @PatrickSteele makes a very good point: how is AutoMapper supposed to map a source list to a dest list of existing objects, when the dest list may not necessarily bear any resemblance to the source list? i.e. "But what if one list has 3 and the other list has 5?"
If you are sure that FirstClass
and SecondClass
have the same number of Children
, and if the FirstClass
's Nth Child always corresponds to SecondClass
's Nth child, you could try something like this:
Mapper.CreateMap()
.ForMember(m => m.Children, o => o.Ignore())
.AfterMap((src, dest) =>
{
for (var i = 0; i < dest.Children.Count; i++)
Mapper.Map(src.Children[i], dest.Children[i]);
});
or if FirstChildProp
is some kind of unique key:
Mapper.CreateMap()
.ForMember(m => m.Children, o => o.Ignore())
.AfterMap((src, dest) =>
{
foreach (var dChild in dest.Children)
{
var sChild = src.Children.Single(c => c.FirstChildProp == dChild.FirstChildProp);
Mapper.Map(sChild, dChild);
}
});