title: Writing a basic Expression Builder
body: <p>I am using Linq-to-Entities and one query requires a property to be loaded dynamically. The query looks like this:</p>
  
  <pre><code>var found = Context.IntegrateViews.GroupBy(x =&gt; x.TypeOfBed)
                     .Select(type =&gt; new TypeCounts
                              { Name = type.Key, Count = type.Count() }).ToList();
  </code></pre>
  
  <p>The property on which Group By clause is being run is TypeOfBed. Now I want to run this query on different properties e.g., next time I want to run it on, let's say x.TypeOfChair and so on. For this, I need to have this property in there dynamically.</p>
  
  <p>I am trying to write a Expression builder which is like this.</p>
  
  <pre><code>public Expression&lt;Func&lt;LookupFilterItem&gt;&gt; PropertyLambda(string propertyName)
  {
      var param = Expression.Parameter(typeof(LookupFilterItem), "lookupItem");
      //type of x in above example is LookupFilterItem
      var propertyExpression = Expression.Property(param, propertyName);
  
      var returnExpr = Expression.Lambda&lt;Func&lt;LookupFilterItem&gt;&gt;(propertyExpression);
      return returnExpr;
      //If I Pass string "TypeOfBed" to this method, I am expecting it to return something like
      // lookupItem =&gt; lookupItem.TypeOfBed
  }
  </code></pre>
  
  <p>And I intend to use it like this:</p>
  
  <pre><code>var found = Context.IntegrateViews.GroupBy(PropertyLambda("TypeOfBed"))
                     .Select(type =&gt; new TypeCounts
                              { Name = type.Key, Count = type.Count() }).ToList();
  </code></pre>
  
  <p>But this is not working, I am getting a compile time error which says:</p>
  
  <blockquote>
    <p>error CS0411: The type arguments for method
    'System.Linq.Queryable.GroupBy(System.Linq.IQueryable,
    System.Linq.Expressions.Expression>)' cannot
    be inferred from the usage. Try specifying the type arguments
    explicitly.</p>
  </blockquote>
  
  <p>What am I doing wrong here?</p>