title: Flatten Nested List using AutoMapper
body: <p>I'm trying to flatten a nested object into a DTO object in .NET 3.5.  Most of what I've seen so far is to use <code>AutoMapper</code> to do this (using v1.x since I need to use .NET 3.5, unfortunately):</p>
  
  <p>Here's what a snippet of my class structures look like:</p>
  
  <pre><code>public class RootObject
  {
      [JsonProperty("BaseSupplier")]
      public BaseSupplier BaseSupplier { get; set; }
  
      [JsonProperty("BaseOrderShipmentLineitem")]
      public IList&lt;BaseOrderShipmentLineitem&gt; BaseOrderShipmentLineitem { get; set; }
  }
  
  public class BaseSupplier
  {
      [JsonProperty("id")]
      public int Id { get; set; }
  
      [JsonProperty("name")]
      public string Name { get; set; }
  }
  
  public class BaseOrderShipmentLineitem
  {
      [JsonProperty("id")]
      public int Id { get; set; }
  
      [JsonProperty("qty_delivered")]
      public int QtyDelivered { get; set; }
  
      [JsonProperty("BaseOrderLineitem")]
      public BaseOrderLineitem BaseOrderLineitem { get; set; }    
  }
  
  public class BaseOrderLineitem
  {
      [JsonProperty("id")]
      public int Id { get; set; }
  
      [JsonProperty("product_sku")]
      public string ProductSku { get; set; }
  }
  
  public class ShipmentDetailsDTO
  {
      public int BaseOrderShipmentLineitemId { get; set; }
      public string BaseSupplierName { get; set; }
      public string Sku { get; set; }
  }
  </code></pre>
  
  <p>I've been trying something like this:</p>
  
  <pre><code>Mapper.CreateMap&lt;BaseOrderLineitem, ShipmentDetailsDTO&gt;()
      .ForMember(d =&gt; d.Sku, opts =&gt; opts.MapFrom(s =&gt; s.ProductSku));
  Mapper.CreateMap&lt;BaseOrderShipmentLineitem, ShipmentDetailsDTO&gt;();
  Mapper.CreateMap&lt;RootObject, ShipmentDetailsDTO&gt;()
      .ForMember(d =&gt; d.Sku, opts =&gt; opts.MapFrom(s =&gt; Mapper.Map&lt;IEnumerable&lt;BaseOrderLineitem&gt;, IEnumerable&lt;ShipmentDetailsDTO&gt;&gt;(s.BaseOrderShipmentLineitem.SelectMany(q =&gt; q.BaseOrderLineitem)).FirstOrDefault().Sku))
      ;
  
  var model = Mapper.Map&lt;IEnumerable&lt;RootObject&gt;, IEnumerable&lt;ShipmentDetailsDTO&gt;&gt;(obj);
  </code></pre>
  
  <p>With that above code I'm getting an error on this bit <code>s.BaseOrderShipmentLineitem.SelectMany(q =&gt; q.BaseOrderLineitem)</code>:</p>
  
  <pre><code>Cannot implicitly convert type 'IEnumerable&lt;?&gt;' to 'IEnumerable&lt;BaseOrderLineitem&gt;'. An explicit conversion exists (are you missing a cast?)
  </code></pre>
  
  <p>I'm not sure if it's something simple I'm just overlooking or not.</p>