title: Using Linq expressions as a specification pattern with parent/child query
body: <p>I'm trying to use the specification pattern implemented as a Linq expression so that Linq providers can parse it to produce efficient database queries.  </p>
  
  <p><a href="https://web.archive.org/web/20081120213849/http://www.mostlyclean.com/post/2008/08/Linq-Expressions-The-Specification-Pattern-and-Repositories-Part-1.aspx" rel="nofollow noreferrer">This</a> gives the basic idea.</p>
  
  <p>I am having a hard time trying trying to get it working with a parent/child query </p>
  
  <pre><code>class Parent
  {
      public int Foo;
  
      public IList&lt;Child&gt; Children = new List&lt;Child&gt;();
  }
  
  class Child
  {
      public int Bar;
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          IQueryable&lt;Parent&gt; qry = GetQry(); //initialised
  
  
          //This works but duplicates the IsBigBar() logic
          //Included to show what I am trying to query on
          var parentsWithBigChildBars =
                  from parents in qry
                  where parents.Children.Any(child =&gt; child.Bar &gt; 10) 
                  select parents;
  
          var parentsWithBigChildBars2 =
                 from parents in qry
                 where parents.Children.Any( ?? ) //but how do i access my IsBigBar() expression from here?
                 select parents;
      }
  
  
      //I want to re-use it to pull parents back!
      public Expression&lt;Func&lt;Child, bool&gt;&gt; IsBigBar()
      {
          return child =&gt; child.Bar &gt; 10;
      }
  
      //I'f i use this as the Any() delegate, it compiles &amp; runs but not an expression so evaluated client side
      public Func&lt;Child, bool&gt; IsBigBar2()
      {
          return child =&gt; child.Bar &gt; 10;
      }
  }
  </code></pre>