title: How do I create an dynamic expression like ( x, y ) => DateTime.Compare( y.EFFECTIVE_DATE, x.EFFECTIVE_DATE )
body: <p>I am a newer coder and am having a lot of issues creating the following lamdba expression dynamically using expression trees.</p>
  
  <pre><code>( x, y ) =&gt; DateTime.Compare( y.EFFECTIVE_DATE, x.EFFECTIVE_DATE )
  </code></pre>
  
  <p>I am just starting to understand the basics of the Expression Trees but don't seem to know enough to create this. I don't have any good code to really put here because honestly I am really fumbling around with this.  Any help would be great.</p>
  
  <p>Thank you in advance. </p>
  
  <p>Sorry, first time posting and having some issues with formatting items.</p>
  
  <p>So, In my main method I have the following:</p>
  
  <p>var sortExpression = testing.GetExpression( "EFFECTIVE_DATE", "EFFECTIVE_DATE" );</p>
  
  <pre><code>        if (e.SortDirection == SortDirection.Ascending)
          {
              paymentAuthorizationList.Sort( sortExpression );  
          }
  </code></pre>
  
  <p>And, in my Get Expression Method:</p>
  
  <pre><code>    public Expression&lt;Func&lt;AuthorizePayments, AuthorizePayments, int &gt;&gt; GetExpression&lt;t&gt;( string propertyName, string propertyValue )
      {
  
          var paramX = Expression.Parameter( typeof( AuthorizePayments ), "x" ); // x
          var paramY = Expression.Parameter( typeof( AuthorizePayments ), "y" ); // y
  
          var xDate = Expression.Property( paramX, "EFFECTIVE_DATE" ); // x.EFFECTIVE_DATE
          var yDate = Expression.Property( paramY, "EFFECTIVE_DATE" ); // y.EFFECTIVE_DATE
  
          // DateTime.Compare(y.EFFECTIVE_DATE, x.EFFECTIVE_DATE)
          var body = Expression.Call( typeof( DateTime ).GetMethod( "DateTime.Compare" ), yDate, xDate );
  
          // (x, y) =&gt; DateTime.Compare(y.EFFECTIVE_DATE, x.EFFECTIVE_DATE)
          var expr = Expression.Lambda&lt;Func&lt;AuthorizePayments, AuthorizePayments, int &gt;&gt;( body, paramX, paramY );
  </code></pre>
  
  <p>return expr;
  }</p>
  
  <p>I know the variables are wrong at this time.  The sort expression will not work for the sort method.</p>
  
  <p>Am i just way off base for what I am trying to do?</p>
  
  <p>Sorry for my ignorance and thanks for the help.  </p>