AutoMapper only requires configuration of element types, not of any array or list type that might be used. For example, we might have a simple source and destination type:

public class Source
{
	public int Value { get; set; }
}

public class Destination
{
	public int Value { get; set; }
}

All the basic generic collection types are supported:

Mapper.CreateMap<Source, Destination>();

var sources = new[]
	{
		new Source { Value = 5 },
		new Source { Value = 6 },
		new Source { Value = 7 }
	};

IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources);
ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources);
IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);
List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources);
Destination[] arrayDest = Mapper.Map<Source[], Destination[]>(sources);

To be specific, the source collection types supported include:
  • IEnumerable
  • IEnumerable<T>
  • ICollection
  • ICollection<T>
  • IList
  • IList<T>
  • List<T>
  • Arrays
For the non-generic enumerable types, only unmapped, assignable types are supported, as AutoMapper will be unable to "guess" what types you're trying to map. As shown in the example above, it's not necessary to explicitly configure list types, only their member types.

As of release 0.2.0, no custom destination collection types are supported.

Polymorphic element types in collections

Many times, we might have a hierarchy of types in both our source and destination types. AutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.

public class ParentSource
{
	public int Value1 { get; set; }
}

public class ChildSource : ParentSource
{
	public int Value2 { get; set; }
}

public class ParentDestination
{
	public int Value1 { get; set; }
}

public class ChildDestination : ParentDestination
{
	public int Value2 { get; set; }
}

AutoMapper still requires explicit configuration for child mappings, as AutoMapper cannot "guess" which specific child destination mapping to use. Here is an example of the above types:

Mapper.CreateMap<ParentSource, ParentDestination>()
	.Include<ChildSource, ChildDestination>();
Mapper.CreateMap<ChildSource, ChildDestination>();

var sources = new[]
	{
		new ParentSource(),
		new ChildSource(),
		new ParentSource()
	};

var destinations = Mapper.Map<ParentSource[], ParentDestination[]>(sources);

destinations[0].ShouldBeInstanceOf<ParentDestination>();
destinations[1].ShouldBeInstanceOf<ChildDestination>();
destinations[2].ShouldBeInstanceOf<ParentDestination>();

Last edited Feb 23, 2009 at 3:31 AM by jbogard, version 1

Comments

humergu Dec 14, 2010 at 11:06 AM 
.ForMember(dest => dest.DestPropertyToBeSkipped, opt => opt.Ignore())

GerhardKreuzer Sep 2, 2010 at 10:44 AM 
What's about lazy loading? I use openAccess by Telerik which does this since the first moment, now EF still get such a feature too.
Do we lost all of that, when we map Lists?????

bartelink Aug 3, 2010 at 1:53 PM 
Any chance you can put the "Mapper.CreateMap<ChildSource, ChildDestination>();" bit before the include mapping and/or state it in the text - I got caught by the fact that the child mapping needs to be registered, in addition to the .Include bit. (Not suggesting for one second that we're actually going to find another person that'll be that stupid :D)

mjezzi Apr 7, 2010 at 7:09 PM 
Suggestion:

I have a scenario where I want some of my child classes to be skipped completely (they're not needed in my views). It would be nice to have some sort of operation similar to the following:

Mapper.CreateMap<ParentSource, ParentDestination>()
.Include<ChildSource, ChildDestination>()
.Ignore<ChildSource2>();