title: Lambda Expression to be used in Select() query
body: <p>I am trying to build a lambda expression, containing two assignments (as shown further down), that I can then pass to a Queryable.Select() method. </p>
  
  <p>I am trying to pass a string variable into a method and then use that variable to build up the lambda expression so that I can use it in a LINQ Select query. </p>
  
  <p>My reasoning behind it is that I have a SQL Server datasource with many column names, I am creating a charting application that will allow the user to select, say by typing in the column name, the actual column of data they want to view in the y-axis of my chart, with the x-axis always being the DateTime. Therefore, they can essentially choose what data they chart against the DateTime value (it’s a data warehouse type app).</p>
  
  <p>I have, for example, a class to store the retrieved data in, and hence use as the chart source of:</p>
  
  <pre><code>public class AnalysisChartSource
  {
      public DateTime Invoicedate { get; set; }
      public Decimal yValue { get; set; }
  }
  </code></pre>
  
  <p>I have (purely experimentaly) built an expression tree for the Where clause using the String value and that works fine:</p>
  
  <pre><code>public void GetData(String yAxis)
  {
      using (DataClasses1DataContext db = new DataClasses1DataContext())
      {
          var data = this.FunctionOne().AsQueryable&lt;AnalysisChartSource&gt;();
          //just to get some temp data in....
  
          ParameterExpression pe = Expression.Parameter(typeof(AnalysisChartSource), "p");
          Expression left = Expression.MakeMemberAccess(pe,
                                                  typeof(AnalysisChartSource).GetProperty(yAxis));
          Expression right = Expression.Constant((Decimal)16);
          Expression e2 = Expression.LessThan(left, right);
          Expression expNew = Expression.New(typeof(AnalysisChartSource));
  
          LambdaExpression le = Expression.Lambda(left, pe);
  
          MethodCallExpression whereCall = Expression.Call(
              typeof(Queryable), "Where", new Type[] { data.ElementType },
              data.Expression,
              Expression.Lambda&lt;Func&lt;AnalysisChartSource, bool&gt;&gt;(e2, new ParameterExpression[] { pe }));
      }
  }
  </code></pre>
  
  <p>However……I have tried a similar approach for the Select statement, but just can’t get it to work as I need the Select() to populate both X and Y values of the AnalysisChartSource class, like this:</p>
  
  <pre><code>.Select(c =&gt; new AnalysisChartSource 
  { Invoicedate = c.Invoicedate, yValue = c.yValue}).AsEnumerable();
  </code></pre>
  
  <p>How on earth can I build such an expression tree….or….possibly more to the point…..is there an easier way that I have missed entirely?</p>