title: IQueryable LINQ cast error for group by using dynamic expression
body: <p>I'm using C#, .Net 4.5, MVC, entity framework 5.0 with code first. I've encountered error on using one of the examples from the devexpress. The problem lies on getting a list of groupby value and aggregate (count) queries. </p>
  
  <p>The error is <strong>Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casing EDM primitive or enumeration types</strong>. </p>
  
  <p>The entity/table is </p>
  
  <pre><code>public class Test
  {
      [Key]
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime SubmitDate { get; set; }
      public int TotalValue { get; set; }
  }
  </code></pre>
  
  <p>Test code on getting the grouping information</p>
  
  <pre><code>public void GetGroupInfo() 
  {
      GetGroupInfo(Context.Tests, "TotalValue");
      GetGroupInfo(Context.Tests, "Name");
  }
  
  public static void GetGroupInfo(this IQueryable query, string fieldName)
  {
      CriteriaToExpressionConverter converter = new CriteriaToExpressionConverter();
  
      var rowType = query.ElementType;
      query = query.MakeGroupBy(converter, new OperandProperty(fieldName));
      query = query.MakeOrderBy(converter, new ServerModeOrderDescriptor(new OperandProperty("Key"), false));
  
      /*
        i think the problem is from here
      */
      query = ApplyExpression(query, rowType, "Key", "Count");
  
      // ignore the GridViewGroupInfo, just a class to store the value
      var list = new List&lt;GridViewGroupInfo&gt;();
      foreach (var item in query)
      {
          var obj = (object[])item;
          list.Add(new GridViewGroupInfo() {KeyValue=obj[0], DataRowCount =(int)obj[1]});
      }
  }
  
  static IQueryable ApplyExpression(IQueryable query, Type rowType, params string[] names)   
  {
      var parameter = Expression.Parameter(query.ElementType, string.Empty);
      var expressions = names.Select(n =&gt; query.GetExpression(n, rowType, parameter));
      var arrayExpressions = Expression.NewArrayInit(
          typeof(object),
          expressions.Select(expr=&gt;Expression.Convert(expr,typeof(object))).ToArray()
      );
      var lambda = Expression.Lambda(arrayExpressions, parameter);
  
      var expression = Expression.Call(
          typeof(Queryable),
          "Select",
          new Type[] { query.ElementType, lambda.Body.Type },
          query.Expression,
          Expression.Quote(lambda)
      );
      return query.Provider.CreateQuery(expression);
  }
  
  static Expression GetExpression(this IQueryable query, string commandName, Type rowType,    ParameterExpression parameter)
  {
      switch (commandName)
      {
          case "Key":
              return Expression.Property(parameter, "Key");
          case "Count":
              return Expression.Call(typeof(Enumerable), "Count", new Type[] { rowType }, parameter);
      }
      return null;
  }
  </code></pre>
  
  <p>It gives me error regardless the grouping is on the "Name" (string) type or the "TotalValue" (int) type. Anyone can help? Appreciate if anyone tells me why, what and how since I'm still learning about this entire .net, mvc &amp; linq.</p>