title: Automapper: Auto map collection property for a dto object
body: <p>I have a domain object</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public class ProductModel
  {
      public long Id {get;set;}
      public string Name {get;set;}
      public string SerialNumber {get;set;}
  }
  </code></pre>
  
  <p>Single Dto class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ProductDto
  {
      public long Id {get;set;}
      public string Name {get;set;}
      public string SerialNumber {get;set;}
  }
  </code></pre>
  
  <p>Single Dto class that is a list of Dto object:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ProductListDto : List&lt;ProductDto&gt;
  {
      public List&lt;ProductDto&gt; Products;
  
      public ProductListDto()
      {
          Products = new List&lt;ProductDto&gt;();
      }
  }
  </code></pre>
  
  <p>And I'd like to map a list of domain objects to list of Dto objects such that the "Products" property of ProductListDto object AUTOMATICALLY is mapped with a list of  ProductModel objects:</p>
  
  <pre class="lang-cs prettyprint-override"><code>ProductListDto dto = new ProductListDto();  
  
  Mapper.CreateMap&lt;ProductModel, ProductDto&gt;();
  
  /* dto = (ProductListDto) Mapper.Map&lt;List&lt;ProductModel&gt;, List&lt;ProductDto&gt;&gt;((List&lt;ProductModel&gt;)model);  this code line causes error. It is commented out. */ 
  
  dto.Products = Mapper.Map&lt;List&lt;ProductModel&gt;, List&lt;ProductDto&gt;&gt;((List&lt;ProductModel&gt;)model);  // (*)  works OK but need to specify "Products" property
  </code></pre>
  
  <p>The code line (*) works OK, but I'd like to know if there is another way to AUTOMATICALLY (implicitly) map that "Products" property of dto object other than the code line (*)?</p>
  
  <p>That means I do not have to write code like the left hand side of code line (*).</p>