title: Automapper: how do I iterate through a collection
body: <p>Right now I have some models that look like this:</p>
  
  <p>Consumer ResourceModel:</p>
  
  <pre><code>    public ConsumerResourceModel()
      {
          consumerData = new List&lt;ConsumerData&gt;();
          Memberships = new List&lt;MemberResourceModel&gt;();
      }
  
      public List&lt;ConsumerData&gt; consumerData { get; set; }
      public List&lt;MemberResourceModel&gt; Memberships { get; set; }
  </code></pre>
  
  <p>ConsumerResponseModel:</p>
  
  <pre><code> public class ConsumerResponseModel
  {
      public List&lt;Consumer&gt; consumers { get; set; }
  }
  
  public class Membership
  {
      public string membershipType { get; set; }
  }
  
  public class Consumer
  {
      public decimal consumerId { get; set; }
      public Name name { get; set; }
      public List&lt;Membership&gt; memberships { get; set; }
  }
  </code></pre>
  
  <p>My current Mapping looks like this:</p>
  
  <pre><code> public static class AutoMapperConfig
  {
      public static void RegisterMappings()
      {
          Mapper.Initialize(config =&gt;
          {
              config.CreateMap&lt;ConsumerData, Consumer&gt;()                    
                  .ForMember(dest=&gt;dest.memberships, opts=&gt;opts.MapFrom(src=&gt;new List&lt;Membership&gt;()))
                  .ReverseMap();
  
              config.CreateMap&lt;ConsumerData, Name&gt;()
                  .ReverseMap();
  
              config.CreateMap&lt;MemberResourceModel, Membership&gt;()  
                  .ReverseMap();
          });
      }
  </code></pre>
  
  <p>I have everything being mapped correctly for consumer and membership but I'm basically having to do something like this in my method:</p>
  
  <pre><code>        public ConsumerResponseModel MapConsumer(IEnumerable&lt;ConsumerResourceModel&gt; res)
      {
          var responseModel = new ConsumerResponseModel {consumers = new List&lt;Consumer&gt;()};
          var consumerResourceModels = res as IList&lt;ConsumerResourceModel&gt; ?? res.ToList();
          foreach (var item in consumerResourceModels)
          {
              foreach(var consumerData in item.consumerData)
              {
                    var consumer = Mapper.Map&lt;Consumer&gt;(consumerData);
  
                    foreach(var membership in item.Memberships)
                    {
                         consumer.memberships.Add(Mapper.Map&lt;Membership&gt;(membership);
                    }
                  responseModel.consumers.Add(consumer);
               }
          }
          return responseModel;
      }
  </code></pre>
  
  <p>While this code is doing the mapping I need it to do and everything works fine, I'm trying to figure out how I can make one call to Mapper.Map and it just automaps everything in this collection for me instead of having this extra method iterating through and just have it all being done in my AutoMapperConfig class.</p>
  
  <p>I'm having a tough time wrapping my head around how to get that piece to work as everything I have tried like doing .AfterMap in the ConsumerData, Consumer mapping just ends up creating empty memberships. I haven't mapped ConsumerResourceModel yet as I figured once I learned how to map the memberships then I could move a level up to map that all correctly.</p>