title: EF Core - Expression Tree Equivalent for IQueryable Search
body: <p>I have an initial workflow put together that allows me to perform inclusive searches for string properties of objects contained in an <code>IQueryable</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static IQueryable ApplySearch(this IQueryable queryable, string search)
  {
      // validation omitted for brevity
      var expression = queryable
          .Cast&lt;object&gt;()
          .Where(item =&gt; item.SearchStringTree(search))
          .Expression;
  
      var result = queryable.Provider.CreateQuery(expression);
      return result;
  }
  
  static bool SearchStringTree&lt;T&gt;(this T value, string search) =&gt;
      value.GetObjectStrings().Any(s =&gt; s.Contains(search.ToLower()));
  
  static IEnumerable&lt;string&gt; GetObjectStrings&lt;T&gt;(this T value)
  {
      var strings = new List&lt;string&gt;();
  
      var properties = value.GetType()
          .GetProperties()
          .Where(x =&gt; x.CanRead);
  
      foreach (var prop in properties)
      {
          var t = prop.PropertyType.ToString().ToLower();
          var root = t.Split('.')[0];
  
          if (t == "system.string")
          {
              strings.Add(((string)prop.GetValue(value)).ToLower());
          }
          else if (!(root == "system"))
          {
              strings.AddRange(prop.GetValue(value).GetObjectStrings());
          }
      }
  
      return strings;
  }
  </code></pre>
  
  <p>Would it be possible to apply this concept in a way that Entity Framework can translate prior to DbContext execution?</p>
  
  <p>I've been looking into potentially using <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/how-to-use-expression-trees-to-build-dynamic-queries" rel="nofollow noreferrer">Expression Trees</a> to accomplish this.</p>
  
  <p>Here's a working <a href="https://repl.it/@JaimeStill/IQueryableSearch" rel="nofollow noreferrer">Repl.it</a> showing the <code>IQueryable</code> implementation above.</p>