title: How do I dynamically construct an Expression Tree to use with anonymous types
body: <p>This is my first post here. If I've broken any guidelines, please let me know and I'll be happy to correct them. </p>
  
  <p>I have the following entity classes:</p>
  
  <pre><code>public class Book
  {
  public int BookID { get; set; }
  public string Author { get; set; }
  public string Publisher { get; set; }
  }
  
  </code></pre>
  
  <p>And a second entity class as such,</p>
  
  <pre><code>public class Library
  {
  public int ID  { get; set; } 
  public Book Book { get; set; }
  public int Count { get; set; }
  }
  </code></pre>
  
  <p>I also have this function to generate a lambda expression dynamically based on user input. </p>
  
  <pre><code>public static Expression&lt;Func&lt;T, bool&gt;&gt; GetLambdaExpression&lt;T&gt;(List&lt;Operation&gt; OperationList)
          {  
              ExpressionTree expressionTree = new ExpressionTree();
              Node Root = expressionTree.ConstructTree(OperationList);
  
              var Parameter = Expression.Parameter(typeof(T), "x");
              var Expression = CalculateExpression(Root);  //Returns an Expression Clause by And/Or every input in OperationList
              return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression, Parameter); //Finally creating Lambda
          }
  
  
  </code></pre>
  
  <p>Operation class contains details about the type of operation, field and values. It gets passed from the client which I'm using the query against the Entity classes by mapping field names.</p>
  
  <p>code works as intended when used this way,</p>
  
  <pre><code>var OperationList = //Get from client
  var LambdaExpression = GetLambdaExpression&lt;Book&gt;(OperationList);
  var result = _BookContext.Books.Where(LambdaExpression);
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>var OperationList = //Get from client
  var LambdaExpression = GetLambdaExpression&lt;Library&gt;(OperationList);
  var result = _LibraryContext.Library.Where(LambdaExpression);
  
  </code></pre>
  
  <p>I'm trying to Join two Entity classes using LINQ but I can't seem to find a way to dynamically create a Lambda Expression for the generated Anonymous type that is returned by the JOIN. </p>
  
  <p>My join looks like,</p>
  
  <pre><code> var result = from c in _BookContext.Books
  
                join d in _LibraryContext.Library
  
                on c.BookID equals d.ID
  
                select new { c , d };
  
  </code></pre>
  
  <p>However, this won't work for obvious reasons,</p>
  
  <pre><code>var OperationList = //Passed from client
  var LambdaExpression = GetLambdaExpression&lt;T&gt;(OperationList);
  result.Where(LambdaExpression); 
  
  </code></pre>
  
  <p>Passing 'object' or 'dynamic' to the GetLambdaExpression() doesn't work because the field names are not pre-defined and it throws an exception. </p>
  
  <p>How could I construct an Expression Tree for an Anonymous type. </p>
  
  <p>Many thanks. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I managed to fix it. Here's what I did:</p>
  
  <p>Instead of storing the result of a join into an Anonymous type, I created a new class which has objects of the Entity classes used to perform the join.</p>
  
  <pre><code>public class JoinResult
      {
          public Book Book { get; set; }
          public Library Library { get; set; }
      }
  </code></pre>
  
  <p>Perform the join and store the data into JoinResult</p>
  
  <pre><code>var result = from c in _BookContext.Books
  
                join d in _LibraryContext.Library
  
                on c.BookID equals d.ID
  
                select new JoinResult{ Book = c , Library = d };
  </code></pre>
  
  <p>Finally, here's the trick to dynamically create the Lambda Expression for JoinResult. </p>
  
  <p>I created an Expression Parameter for JoinResult, then created Expression Properties for the Properties of JoinResult. </p>
  
  <p>I used the Expression Properties created to be used as Parameters to be passed into a new Property for the Entity class. Essentially, creating a property in the format of "x.Book.BookID".</p>
  
  <p>For instance, if I wanted to perform a EqualOperation on JoinResult. Here is how I would do it. </p>
  
  <pre><code>public static IQueryable&lt;T&gt; PerformEqualOperation&lt;T&gt;(int Constant, int FieldName, Type Prop, IQueryable&lt;T&gt; resultOfJoin)
          {
  
              var Parameter = Expression.Parameter(typeof(T), "x"); //x
              PropertyInfo[] Properties = typeof(T).GetProperties(); //Get the properties of JoinResult
  
              string propertyname; 
  
              //Get the property name
              foreach(var property in Properties)
              {
                 if(property.GetType() == Prop)
                    propertyname = property.Name;
              }
  
             //Creating a property that can be passed as a parameter to the property for Entity class.
             var expressionparameter = Expression.Property(Parameter, propertyname);  //x.Book
                  var expressionproperty = Expression.Property(expressionparameter, FieldName);//x.Book.BookID
                  var expressionclause = Expression.Equal(expressionproperty, Expression.Constant(Constant));//x.Book.BookID == Constant
  var expressionlambda = Expression.Lambda&lt;Func&lt;T,bool&gt;&gt;(expressionclause, Parameter) 
                  return resultOfJoin.Where(expressionlambda).AsQueryable();
          }
  </code></pre>
  
  <p>Hope this helps</p>