title: How do I select recursive nested entities using LINQ to Entity
body: <p>I have an entity called Category and the entity contains a IEnumerable called ChildCategories. A category can have these child categories which can have it's own child categories and so on.</p>
  
  <p>Say I have selected out the top level parent category, I want to get all child categories and their child categories and so on so that I have all hierarchical children of the category. I want this flatterned and returned with the initial category. I've tried creating something like</p>
  
  <pre><code>    public static IEnumerable&lt;T&gt; AllChildren&lt;T&gt;(this IEnumerable&lt;T&gt; items, 
          Func&lt;T, IEnumerable&lt;T&gt;&gt; children, bool includeSelf)
      {
          foreach (var item in items)
          {
              if (includeSelf)
              {
                  yield return item;
              }
              if (children != null)
              {
                  foreach (var a in children(item))
                  {
                      yield return a;
                      children(a).AllChildren(children, false);
                  }
              }
          }
      }
  </code></pre>
  
  <p>Which would get flatterned after using SelectMany method but havn't quite got it.</p>