title: Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
body: <p>Given a simple class with arbitrary properties (for discussion lets say Id, Name, and Description)</p>
  
  <p>and given an instance of that class, I want to find matching entries in the database by specifying the property to match</p>
  
  <p>I'm trying to do something in this respect similar to the AddOrUpdate method of EF, but I need the entity returned to me for further processing.</p>
  
  <pre><code>var match = new SomeClass{Name="Whatever"};
  var found = Context.SomeClass.Find(x=&gt;x.Name, match);
  
  public static T Find&lt;T&gt;(this DbSet&lt;T&gt; set, Expression&lt;Func&lt;T, object&gt;&gt; matchOn, T matchAgainst) where T : class {
    var func = matchOn.Compile();
    var valueToFind = func(matchAgainst);
  
  
   var combinedExpression = //matchon + "=" + valueToFind;
   var found = set.FirstOrDefault(combinedExpression);
   return found;
   }
  </code></pre>
  
  <p>That gives me the value of the property in the passed in object, but I need to now combine that value with the passed in expression and pass it to the db set. </p>
  
  <p>IE, the code I'm effectively trying to run is  <code>set.FirstOrDefault(x=&gt;x.Name==valueToFind)</code> How do I take the <code>matchon</code> expression (which contains <code>x=&gt;x.Name</code>) and combine that with the <code>==valueToFind</code> to get the <code>x=&gt;x.Name==valueToFind</code> from them?</p>
  
  <p>How do I build the combined expression? (I realize the "string" code above is completely wrong, but I was trying to get across what I need the function to do, but I have no idea what that syntax would look like.)</p>
  
  <p>For manually coded examples, it would be easy enough just to pass in a hardcoded lambda with the value set, but my use case involves running through a collection of objects and finding the match for each one, so the value will not be known until runtime, and the method must work against arbitrary types and various properties, so I can't hardcode the property name either. </p>