title: Dynamically filtering a column/property in to an EF 4.1 query using C#
body: <p>I'm trying to create a generic "search engine" in C# using linq. I have a simple search engine that functions and look like the following.</p>
  
  <pre><code>var query = "joh smi";
  var searchTerms = query.Split(new char[] { ' ' });
  var numberOfTerms = searchTerms.Length;
  
  var matches = from p in this.context.People
                from t in searchTerms
                where p.FirstName.Contains(t) ||
                p.LastName.Contains(t)
                group p by p into g
                where g.Count() == numberOfTerms
                select g.Key;
  </code></pre>
  
  <p>I want it to be more generic so I can call it like this: </p>
  
  <pre><code>var matches = Search&lt;Person&gt;(dataset, query, p =&gt; p.FirstName, p =&gt; p.LastName);
  </code></pre>
  
  <p>I've gotten as far as the following, but it fails with a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities." System.NotSupportedException.</p>
  
  <pre><code>static IEnumerable&lt;T&gt; Find&lt;T&gt;(IQueryable&lt;T&gt; items, string query,
                                params Func&lt;T, string&gt;[] properties)
  {
      var terms = query.Split(' ');
      var numberOfParts = terms.Length;
  
      foreach (var prop in properties)
      {
          var transformed = items.SelectMany(item =&gt; terms, 
                  (item, term) =&gt; new { term, item });
  
                                                // crashes due to this method call
          var filtered = transformed.Where(p =&gt; prop(p.item).Contains(p.term));
          items = filtered.Select(p =&gt; p.item);
      }
  
      return from i in items
             group i by i into g
             where g.Count() == numberOfParts
             select g.Key;
  }
  </code></pre>
  
  <p>I'm certain it's doable, there just has to be a way to compile <code>i =&gt; i.FirstName</code> to an <code>Expression&lt;Func&lt;T, bool&gt;&gt;</code>, but that's where my LINQ expertise ends. Does anyone have any ideas?</p>