title: Flattening collections with ValueInjector or AutoMapper
body: <p>Given the following sample classes:</p>
  
  <pre><code>public class OrderHeader
  {
      public int OrderHeaderId { get; set; }
      public string CustomerName { get; set; }
  }
  
  public class OrderItem
  {
      public int OrderHeaderId { get; set; }
      public int OrderItemId { get; set; }
      public string ProductName { get; set; }
      public float Price { get; set; }
  }
  
  // Order contains the header information and a collection of items ordered
  public class Order
  {
      public OrderHeader Header { get; set; }
      public IEnumerable&lt;OrderItem&gt; Items { get; set; }
  }
  
  // LineItem is OrderHeader and OrderItem flattened out
  public class LineItem
  {
      public int OrderHeaderId { get; set; }
      public int OrderItemId { get; set; }
      public string CustomerName { get; set; }
      public string ProductName { get; set; }
      public float Price { get; set; }
  }
  </code></pre>
  
  <p>Using the extension method from <a href="https://stackoverflow.com/a/7872763/9664">jgauffin to extend ValueInjector</a> to work with collections, the following will flatten out a collection of <code>Order</code>'s to a collection of <code>LineItem</code>'s:</p>
  
  <pre><code>var orders = new List&lt;Order&gt;();
  
  var lineItems = new List&lt;LineItem&gt;();
  
  // -extension method for collections with ValueInjector
  // -project all the OrderItems into a single list
  lineItems.InjectFrom( orders.SelectMany( x =&gt; x.Items ) );
  
  // inject the header information into the flattened out LineItem objects
  foreach( LineItem item in lineItems )
  {
      var order = orders.Single( x =&gt; x.Header.OrderHeaderId == item.OrderHeaderId );
      item.InjectFrom( order.Header );
  }
  </code></pre>
  
  <p><strong>2 questions:</strong></p>
  
  <ol>
  <li>Is the above pattern a correct/effective usage of using ValueInjector to flatten out a collection of <code>Order</code>'s to a collection of <code>LineItem</code>'s? If not, what would be the alternative? (other than not using the ValueInjector extension method and just using a single for each loop to inject both the Header and OrderItem into the flattened LineItem)</li>
  <li>Is there an equivalent when using AutoMapper?</li>
  </ol>