title: Create Func<T, bool> from MemberExpression and Constant
body: <p>I don't do expression tree work enough to get this working...</p>
  
  <p>Essentially what I want to create is <code>m.MyProperty == 1</code>, to be used in a method that takes <code>Func&lt;T, bool&gt;</code>.</p>
  
  <p>I have a <code>MemberExpression</code> already. I've tried various things, but I just keep getting different errors.</p>
  
  <p>I currently have something like this (that doesn't work):</p>
  
  <pre><code>object const = 1;
  var equalExpression = Expression.Equal( memberExpression, Expression.Constant( const ) );
  var compiled = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( equalExpression, Expression.Parameter( typeof( T ) ).Compile();
  </code></pre>
  
  <p>This gives me an exception:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: variable 'm' of type 'MyType' referenced from scope '', but it is not defined</p>
  </blockquote>
  
  <p>I've tried re-working several different parts of this but haven't come up with anything that works.</p>
  
  <p>The <code>const</code> is an object that can be any type, but should match the type of the <code>MemberExpression</code>.</p>
  
  <p><strong>Solution:</strong></p>
  
  <pre><code>object c = 1;
  var parameterExpression = (ParameterExpression)memberExpression.Expression;
  var equalExpression = Expression.Equal(memberExpression, Expression.Constant(c));
  var compiled = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(equalExpression, parameterExpression).Compile();
  </code></pre>