title: LINQ - filter child collection
body: <p>I'd like to be able to query parent entities and filter the contents of a child collection.</p>
  
  <p>For example, I have a collection of OrderHeaders.  I want to query this collection using LINQ to return all OrderHeaders, but I only want some of the related OrderDetail rows to be included.</p>
  
  <p>I am preferably looking for a solution where I can do all of this in a single LINQ statement.</p>
  
  <p>The following console app demonstrates this.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace LINQ
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;OrderHeader&gt; orders = GetOrderHeaders();
  
              var filteredOrders = from p in orders
                                   where p.Detail.Where(e =&gt; e.StockCode == "STK2").Count() &gt; 0
                                   select p;
  
              foreach (var order in filteredOrders)
              {
                  Console.WriteLine("Account {0} ", order.AccountCode);
  
                  foreach (var detail in order.Detail)
                  {
                      Console.WriteLine("StockCode {0}, Quantity {1}", detail.StockCode, detail.Quantity);
                  }
  
                  Console.WriteLine();
              }
  
              // The above will return the following:
              // Account CUST1
              // StockCode STK1, Quantity 1
              // StockCode STK2, Quantity 2
              //
              // Account CUST2
              // StockCode STK2, Quantity 1
              // StockCode STK4, Quantity 2
  
              // How can I get the following?
              // Account CUST1
              // StockCode STK2, Quantity 2
              //
              // Account CUST2
              // StockCode STK2, Quantity 1
  
              Console.ReadLine();
          }
  
          public static List&lt;OrderHeader&gt; GetOrderHeaders()
          {
              List&lt;OrderHeader&gt; orders = new List&lt;OrderHeader&gt;();
  
              OrderHeader header = 
                  new OrderHeader { 
                      AccountCode = "CUST1", 
                      Detail = new List&lt;OrderDetail&gt;()};
              header.Detail.Add(
                  new OrderDetail { StockCode = "STK1", Quantity = 1 });
              header.Detail.Add(
                  new OrderDetail { StockCode = "STK2", Quantity = 2 });
              orders.Add(header);
  
              header =
                  new OrderHeader
                  {
                      AccountCode = "CUST2",
                      Detail = new List&lt;OrderDetail&gt;()
                  };
              header.Detail.Add(
                  new OrderDetail { StockCode = "STK2", Quantity = 1 });
              header.Detail.Add(
                  new OrderDetail { StockCode = "STK4", Quantity = 2 });
              orders.Add(header);
  
              return orders;
          }
      }
  
      public class OrderHeader
      {
          public string AccountCode { get; set; }
          public List&lt;OrderDetail&gt; Detail {get; set;}
      }
  
      public class OrderDetail
      {
          public string StockCode { get; set; }
          public int Quantity { get; set; }
      }
  
  }
  </code></pre>
  
  <p>Many thanks in advance for any suggestions.</p>
  
  <p>Paul</p>