title: Linq Entity Framework -- can I use a recursive method?
body: <p>Following on from the excellent answer to my previous question:</p>
  
  <p><a href="https://stackoverflow.com/questions/3220155/linq-entity-framework-generic-filter-method">Linq Entity Framework generic filter method</a></p>
  
  <p>I am now trying to understand how I can apply something like recursion to my solution.</p>
  
  <p>To recap, instead of multiple similar declarations of this method:</p>
  
  <pre><code>protected IQueryable&lt;Database.Product&gt; GetActiveProducts( ObjectSet&lt;Database.Product&gt; products ) {
  
      var allowedStates = new string[] { "Active" , "Pending" };
  
      return (
          from product in products
          where allowedStates.Contains( product.State )
              &amp;&amp; product.Hidden == "No"
          select product
      );
  
  }
  </code></pre>
  
  <p>I now have a single implementation that accepts an interface type (IHideable) to operate on:</p>
  
  <pre><code>protected IQueryable&lt;TEntity&gt; GetActiveEntities&lt;TEntity&gt;( ObjectSet&lt;TEntity&gt; entities ) where TEntity : class , Database.IHideable {
  
      var allowedStates = new string[] { "Active" , "Pending" };
  
      return (
          from entity in entities
          where allowedStates.Contains( entity.State )
              &amp;&amp; entity.Hidden == "No"
          select entity
      );
  
  }
  </code></pre>
  
  <p>This works well and the solution is clean and understandable ( big thanks to <a href="https://stackoverflow.com/users/263693/stephen-cleary">https://stackoverflow.com/users/263693/stephen-cleary</a> ).</p>
  
  <p>What I am trying to do now is to apply a similar (or the same?) method to any EntityCollections associated to an EntityObject that also happen to implement IHideable.</p>
  
  <p>I currently make use of GetActiveEntities() like this:</p>
  
  <pre><code>var products = GetActiveEntities( Entities.Products );
  
  return (
  
      from product in products
  
      let latestOrder = product.Orders.FirstOrDefault( 
          candidateOrder =&gt; (
              candidateOrder.Date == product.Orders.Max( maxOrder =&gt; maxOrder.Date )
          )       
      )
  
      select new Product() {
  
          Id = product.Id ,
          Name = product.Name,
          LatestOrder = new Order() {
  
              Id = latestOrder.Id ,
              Amount = latestOrder.Amount,
              Date = latestOrder.Date
  
          }
  
      }
  
  );
  </code></pre>
  
  <p>In this example I would like to have the Orders EntityCollection also filter by GetActiveEntities(), so that the latest order returned can never be a "hidden" one.</p>
  
  <p>Is it possible to have all EntityCollections implementing IHideable to be filtered - maybe by applying some reflection/recursion inside GetActiveEntities() and calling itself? I say recursion because the best solution would go multiple levels deep walking through the entity graph.</p>
  
  <p>This stuff is stretching my brain!</p>
  
  <p><strong>UPDATE #1 (moved my comments up to here)</strong></p>
  
  <p>Thanks Steve.</p>
  
  <p>Making the method accept IQuerable as suggested gives this error: </p>
  
  <pre><code>'System.Data.Objects.DataClasses.EntityCollection&lt;Database.Order&gt;' does not contain a definition for 'GetActiveEntities' and no extension method 'GetActiveEntities' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection&lt;Database.Order&gt;' could be found (are you missing a using directive or an assembly reference?)
  </code></pre>
  
  <p>I presume this is because EntityCollection does not implement IQueryable.</p>
  
  <p>I was able to get further by creating a second extension method that explicitly accepted EntityCollection and returned IEnumerable. That compiled but at runtime gave this error:</p>
  
  <pre><code>LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[Database.Order] GetActiveEntities[Order](System.Data.Objects.DataClasses.EntityCollection`1[Database.Order])' method, and this method cannot be translated into a store expression.
  </code></pre>
  
  <p>I also tried calling AsQueryable() on the EntityCollection and returning IQueryable but the same error came back.</p>