title: Calling .Contains on the output of a Expression<Func<T,string>>
body: <p>I am wanting to implement the following method</p>
  
  <pre><code>public static class Filters
  {
     public static Expression&lt;Func&lt;T,bool&gt;&gt; ContainsText&lt;T&gt;(
        string text, params Expression&lt;Func&lt;T,string&gt;&gt;[] fields)
     {
         //..
     }
  }
  </code></pre>
  
  <p>so that if I wanted to (for example) find anyone whose name contains "Mark" or whose dad's name contains "Mark", I can do something like this:</p>
  
  <pre><code>var textFilter = Filters.ContainsText&lt;Individual&gt;("Mark", i=&gt;i.FirstName, i=&gt;i.LastName, i=&gt;i.Father.FirstName, i =&gt; i.Father.LastName);
  var searchResults = _context.Individuals.Where(textFilter).ToList();
  </code></pre>
  
  <p>My end goal is to be able to create a <code>ContainsTextSpecification</code> to simplify text-based filtering that I can use like so:</p>
  
  <pre><code>var textSpec = new ContainsTextSpecification&lt;Individual&gt;(i=&gt;i.FirstName, i=&gt; i.LastName, i=&gt;i.DepartmentName, i=&gt;i.SSN, i=&gt;i.BadgeNumber);
  textSpec.Text = FormValues["filter"];
  var results = individuals.Find(textSpec);
  </code></pre>
  
  <hr>
  
  <p>I found something that gets me <em>close</em> to what I want <a href="https://stackoverflow.com/questions/278684/how-do-i-create-an-expression-tree-to-represent-string-containsterm-in-c">here</a>, but I want to be able to specify the fields I want to filter by using a <code>Func&lt;T,string&gt;</code> rather than just the name of the field. (<strong>edit: I want to be able to specify the -values- that will be checked, not the name of the fields</strong>) </p>
  
  <pre><code>static Expression&lt;Func&lt;T, bool&gt;&gt; GetExpression&lt;T&gt;(string propertyName, string propertyValue)
  {
      var parameterExp = Expression.Parameter(typeof(T), "type");
      var propertyExp = Expression.Property(parameterExp, propertyName);
      MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
      var someValue = Expression.Constant(propertyValue, typeof(string));
      var containsMethodExp = Expression.Call(propertyExp, method, someValue);
  
      return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(containsMethodExp, parameterExp);
  }
  var results = individualRepo.Get(textSpec);
  </code></pre>