title: LINQ Expression Tree
body: <p>I want to generate expression trees using the API for the following:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var managers = dataContext.Employees.Where(e =&gt; e.Subordinates.Any());
  </code></pre>
  
  <p>Additionally, how do I then generate the expression tree to do this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var managedEmployees = managers.ToDictionary(key =&gt; key.Manager, value =&gt; value.Subordinates.Select(s =&gt; s.FullName));
  </code></pre>
  
  <p>I've come up with the following so far for the .Where(), but it errors because it doesn't like the type parameters in <code>new Type[] { typeof(Func&lt;Employee, IEnumerable&lt;Employee&gt;&gt;) }</code>.</p>
  
  <pre class="lang-cs prettyprint-override"><code>ParameterExpression employeesParameter = Expression.Parameter(typeof(Employee), "e");
  MemberExpression subordinatesProperty = Expression.Property(employeesParameter, typeof(Employee).GetProperty("Subordinates"));
  MethodCallExpression hasSubordinates = Expression.Call(typeof(Enumerable),
    "Any",
    new Type[] { typeof(Employee) },
    subordinatesProperty);
  LambdaExpression whereLambda = Expression.Lambda(hasSubordinates, employeesParameter);
  MethodCallExpression whereExpression = Expression.Call(typeof(Queryable),
    "Where",
    new Type[] { typeof(Func&lt;Employee, IEnumerable&lt;Employee&gt;&gt;) },
    dataContext.Employees.AsQueryable(),
    whereLambda);
  </code></pre>