title: Using Expression<Func<T,bool>> as parameter for new expression
body: <p>I have some code that currently builds up an In statement in SQL. I build an expression, it returns;</p>
  
  <pre><code>value(generic(list[T])).Contains(x =&gt; x.Id)
  </code></pre>
  
  <p>This works fine, so if I have a list of objects;</p>
  
  <pre><code>public class ObjectToSearch 
  {
    public int IdToSearchOn {get;set;}
  }
  </code></pre>
  
  <p>And I want to search for ids 1, 2, 3, it works just fine. My SQL query is great. </p>
  
  <p>Now, I have a need to search a list of nested objects, so I might have;</p>
  
  <pre><code>public class ParentObjectToSearch 
  {
    public IEnumerable&lt;ObjectToSearch&gt; Objects {get;set;}
  }
  </code></pre>
  
  <p>So, looking at some code I found (<a href="https://stackoverflow.com/questions/326321/how-do-i-create-this-expression-tree-in-c">How do I create an expression tree calling IEnumerable&lt;TSource&gt;.Any(...)?</a>) I figured I could adapt the method, and wrap a call to Any or All, and it would work. This worked great, until I actually came to test against the database, I get;</p>
  
  <p><strong>Cannot compare elements of type &#39;System.Collections.Generic.ICollection`1&#39;. Only primitive types (such as Int32, String, and Guid) and entity types are supported.</strong></p>
  
  <pre><code>var collectionType = GetIEnumerableImpl( forCollection.Type );
  
  Type elementType = collectionType.GetGenericArguments( )[0];
  
  MethodInfo method = BaseFilter.GetType( ).GetMethod( "FilterWith" );
  
  MethodInfo genericMethod = method.MakeGenericMethod( new[] { elementType } );
  
  return (genericMethod.Invoke( BaseFilter, null ) as LambdaExpression);
  </code></pre>
  
  <p>FilterWith is the method I'm calling on the original filter, in the hope of getting back my expression. So, it looks like my inner expression is being evaluated incorrectly when combined with the outer expression. What I'm basically aiming for is (I believe);</p>
  
  <pre><code>x =&gt; x.Collection.Contains( y =&gt; new { 1, 3, 3}.Contains( y.Id));
  </code></pre>
  
  <p>If I test the inner filtering separately, it works fine, so I assume it's just how I'm trying to combine the elements, and if I try and use Contains instead of Any or All, I still get the same error. </p>
  
  <p>I've put Entity Framework in the tags, because this is being evaluated as expressions against an entity set, and someone may have experience of doing this.</p>
  
  <p><strong>Update</strong> Having a night to think about it, I think I have a better question;</p>
  
  <p>How do I build a Where expression, so I can build;</p>
  
  <p>x => x.Collection.Where( y => new[] { 1, 3 }.Contains( y.Id)).Count( ) > 0</p>