# Automapper - Mapping a nested collection to a list

I'm using Automapper to do some mapping from XSD-generated serialization object to more sane POCO's.

I'm having an issue with a particular type of mapping.

public class SourceOuterObject
{
public SourceSet SourceSet { get; set; }
}

public class SourceSet
{
public List<SourceObject> SourceList{ get; set; }
}


I want to map this to:

public class TargetOuterObject
{
public List<TargetObject> TargetList{ get; set; }
}


I have tried a wide variety of mapping configurations, but the only thing that I've been able to make work is this:

Mapper.CreateMap<SourceOuterObject, TargetOuterObject>();
Mapper.CreateMap<SourceObject, TargetObject>();
Mapper.CreateMap<SourceSet, List<TargetObject>>()
.ConvertUsing(ss => ss.SourceList.Select(bs => Mapper.Map<SourceObject, TargetObject>(bs)).ToList());


This works but seems to be way more complicated than it should be - I feel dirty calling a mapping within a mapping config.

Is this the best I can hope for or is there a better way?

• You could create a topic on stackoverflow, i think they can help you better. – Jamie Dec 1 '14 at 16:01

Mapper.CreateMap<SourceObject, TargetObject>();