title: How to construct LambdaExpression with conversion
body: <p>I need to sort in ajax response grid by column name. Column value is number stored as a string. </p>
  
  <p>Let's say some trivial class (in real-life situation there is no possibility to modify this class):</p>
  
  <pre><code>class TestObject
  {
      public TestObject(string v)
      {
          this.Value = v;
      }
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>then simple test:</p>
  
  <pre><code>[Test]
  public void LambdaConstructionTest()
  {
     var queryable = new List&lt;TestObject&gt;
                     {
                         new TestObject("5"),
                         new TestObject("55"),
                         new TestObject("90"),
                         new TestObject("9"),
                         new TestObject("09"),
                         new TestObject("900"),
                     }.AsQueryable();
  
      var sortingColumn = "Value";
  
      ParameterExpression parameter = Expression.Parameter(queryable.ElementType);
  
      MemberExpression property = Expression.Property(parameter, sortingColumn);
      //// tried this one: var c = Expression.Convert(property, typeof(double));
  
          LambdaExpression lambda = Expression.Lambda(property, parameter); //// constructs: o=&gt;o.Value
  
          var callExpression = Expression.Call(typeof (Double), "Parse", null, property); 
  
          var methodCallExpression = Expression.Call(
              typeof(Queryable),
              "OrderBy",
              new[] { queryable.ElementType, property.Type },
              queryable.Expression,
              Expression.Quote(lambda)); // works, but sorts by string values.
              //Expression.Quote(callExpression)); // getting: System.ArgumentException {"Quoted expression must be a lambda"}
  
          var querable =  queryable.Provider.CreateQuery&lt;TestObject&gt;(methodCallExpression);
  
          // return querable; // &lt;- this is the return of what I need. 
  }
  </code></pre>
  
  <p>Sorry for not being clear in my first post as @SLaks answer was correct but I do not know how to construct correct lambda expression in this case.</p>
  
  <p><strong>Finally found solution</strong> that is good for anyone who has strings in column and needs to sort by converted double value: (Special thanks for @SLaks his post was an eye opener):</p>
  
  <pre><code>    [Test]
      public void LambdaConstructionTest2()
      {
          // GIVEN
          var queryable = new List&lt;TestObject&gt;
                              {
                                  new TestObject("5"),
                                  new TestObject("55"),
                                  new TestObject("90"),
                                  new TestObject("9"),
                                  new TestObject("09"),
                                  new TestObject("900"),
                              }.AsQueryable();
  
          var sortingColumn = "Value";
  
          // WHEN
          ParameterExpression parameter = Expression.Parameter(queryable.ElementType);
  
          MemberExpression property = Expression.Property(parameter, sortingColumn);
  
          MethodCallExpression callExpression = Expression.Call(typeof (Double), "Parse", null, property);
  
          LambdaExpression lambda = Expression.Lambda(callExpression, parameter); // = {Param_0 =&gt; Parse(Param_0.Value)}
  
          UnaryExpression unaryExpression = Expression.Quote(lambda); // Expression&lt;Func&lt;TestObject,double&gt;&gt; = {Param_0 =&gt; Parse(Param_0.Value)}
  
          var methodCallExpression = Expression.Call(
              typeof (Queryable),
              "OrderByDescending",
              new[] { queryable.ElementType, lambda.ReturnType },
              queryable.Expression,
              unaryExpression);
  
          var querable = queryable.Provider.CreateQuery&lt;TestObject&gt;(methodCallExpression);
  
          // THEN
          var expectedMaxValue = queryable.Max(x =&gt; Convert.ToDouble(x.Value));
          var expectedMinValue = queryable.Min(x =&gt; Convert.ToDouble(x.Value));
  
          var list = querable.ToList();
  
          var actualMaxValue = Convert.ToDouble(list.First().Value);
          var actualMinValue = Convert.ToDouble(list.Last().Value);
          Assert.AreEqual(expectedMaxValue, actualMaxValue);
          Assert.AreEqual(expectedMinValue, actualMinValue);
      }
  </code></pre>