title: Building a query (LINQ) with a sub-query
body: <p>For simplicity sake lets assume I have the following two classes:</p>
  
  <pre><code>public class ComplexClass
  {
      public List&lt;SubClass&gt; SubClasses { get; set; }
      public string Name { get; set; }
  }
  
  public class SubClass
  {
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>I have a <code>List&lt;ComplexClass&gt;</code> and I need to build a query based on some parameters.</p>
  
  <p>It's an easy task if all I need to do is use the <code>Name</code> property of <code>ComplexClass</code>. Here's an example:</p>
  
  <pre><code>    static IQueryable&lt;ComplexClass&gt; GetQuery(string someParameter, string someOtherParameter)
      {
          var query = list.AsQueryable();
          if (!String.IsNullOrEmpty(someParameter))
              query = query.Where(c =&gt; c.Name.StartsWith(someParameter));
          if (!String.IsNullOrEmpty(someOtherParameter))
              query = query.Where(c =&gt; c.Name.EndsWith(someOtherParameter));
          return query;
      }
  </code></pre>
  
  <p>Based on the parameters I have I can add more query elements. Of course the above example is simple, but the actual problem contains more parameters, and that number can grow.</p>
  
  <p>Things aren't as simple if I want to find those <code>ComplexClass</code> instances which have <code>SubClass</code> instances which meet criteria based on parameters:</p>
  
  <pre><code>    static IQueryable&lt;ComplexClass&gt; GetSubQuery(string someParameter, string someOtherParameter)
      {
          var query = list.AsQueryable();
          if (!String.IsNullOrEmpty(someParameter))
              if (!String.IsNullOrEmpty(someOtherParameter))
                  return query.Where(c =&gt; c.SubClasses.Where(sc =&gt; sc.Name.StartsWith(someParameter) &amp;&amp; sc.Name.EndsWith(someOtherParameter)).Any());
              else
                  return query.Where(c =&gt; c.SubClasses.Where(sc =&gt; sc.Name.StartsWith(someParameter)).Any());
          else
              if (!String.IsNullOrEmpty(someOtherParameter))
                  return query.Where(c =&gt; c.SubClasses.Where(sc =&gt; sc.Name.EndsWith(someOtherParameter)).Any());
              else
                  return null;
      }
  </code></pre>
  
  <p>I can no longer just add bits of the query based on each parameter, I now need to write the whole query in one go, and this means I need to check every combination of parameters, which is hardly ideal.</p>
  
  <p>I suspect the key is to build an <code>Expression</code> class and create a lambda expression from that, but I'm not sure how to tackle the problem.</p>
  
  <p>Any suggestions? :)</p>
  
  <p>EDIT:</p>
  
  <p>My initial idea was this:</p>
  
  <pre><code>    static IQueryable&lt;ComplexClass&gt; GetSubQuery(string someParameter, string someOtherParameter)
      {
          var query = list.AsQueryable();
          query = query.Where(c =&gt;
          {
              var subQuery = c.SubClasses.AsQueryable();
              if (!String.IsNullOrEmpty(someParameter))
                  subQuery = subQuery.Where(sc =&gt; sc.Name.StartsWith(someParameter));
              if (!String.IsNullOrEmpty(someOtherParameter))
                  subQuery = subQuery.Where(sc =&gt; sc.Name.EndsWith(someOtherParameter));
              return subQuery.Any();
          });
          return query;
      }
  </code></pre>
  
  <p>This works in my small console test application as it's using LINQ to Objects. Unfortunately, I need to use Entity Framework and LINQ to Entities, which causes an implementation similar to the one above to throw a <code>A lambda expression with a statement body cannot be converted to an expression tree</code> error message.</p>