title: Automapper flattened DTO to Parent with Child Collection
body: <p>I have a flattened DTO which I need to map to a Parent with Children relationship.  I'd like to do this via AutoMapper as I'm using it in other places and it works great.  I've seen examples of mapping a Parent and Child but not when the Child is a collection and the source is a flattened DTO.  I've created some classes that I can use for getting the configuration correct.  Below are my sample classes:</p>
  
  <pre><code>public class Parent
  {
      public int ParentId { get; set; }
      public string ParentName { get; set; }
  
      public List&lt;Child&gt; Children { get; set; }
  }
  public class Child
  {
      public int ChildId { get; set; }
      public string ChildName { get; set; }
  }
  public class ParentChildDTO
  {
      public int ParentId { get; set; }
      public string ParentName { get; set; }
      public int ChildId { get; set; }
      public string ChildName { get; set; }
  }
  </code></pre>
  
  <p>I'm performing the mapper initialization on startup.  I'm not getting any errors until I try to perform the mapping.  Below is my mapper initialization code.  I've kept in the commented out line to show the other way that I've tried to accomplish this:</p>
  
  <pre><code>AutoMapper.Mapper.Initialize(cfg =&gt;
              {
                  cfg.CreateMap&lt;ParentChildDTO, Child&gt;();
                  cfg.CreateMap&lt;ParentChildDTO, Parent&gt;()
                      .ForMember(dest =&gt; dest.Children, opt =&gt; opt.MapFrom(src =&gt; src));
                      //.ForMember(dest =&gt; dest.Children, opt =&gt; opt.MapFrom(src =&gt; new Child { ChildId = src.ChildId, ChildName = src.ChildName }));               
  
              });
  </code></pre>
  
  <p>Below is my code that I'm using for trying to perform the mapping configuration:</p>
  
  <pre><code>ParentChildDTO parentChildDTO = new ParentChildDTO { ParentId = 1, ParentName = "Parent Name", ChildId = 2, ChildName = "Child Name" };
  Parent parent = AutoMapper.Mapper.Map&lt;ParentChildDTO, Parent&gt;(parentChildDTO);
  
  List&lt;LienActivity&gt; mapTest = AutoMapper.Mapper.Map&lt;List&lt;BaseActivityUploadDTO&gt;, List&lt;LienActivity&gt;&gt;(request.Activities);
  </code></pre>
  
  <p>I've considered using a Custom Value Resolver, but was hoping to avoid the complexity and extra code if what I'm doing is possible with the correct configuration.</p>
  
  <p>Here's the error that I get with the above code:</p>
  
  <blockquote>
    <p>Error mapping types.</p>
    
    <p>Mapping types: ParentChildDTO -> Parent </p>
    
    <p>Type Map configuration: ParentChildDTO -> Parent </p>
    
    <p>Property: Children</p>
  </blockquote>