How to write group by query in dynamic linq query

Jan 15, 2014 at 9:05 AM
I am struggling to write 'Group by query' in dynamic linq query.
public IEnumerable<DashboardReport> GetDashTransactionsReports(string dimensionColumn)
        var result = (from t in _dashboardContext.GetTransactionsReports()
                      group t by t.DimensionTransactionYear
                          into g
                          select new DashboardReport()
                              Name = g.Key,
                              Value = g.Sum(s => s.MeasureTotal)

        var resultList = _dashboardContext.GetTransactionsReports().AsQueryable().GroupBy("TransactionMonth", "DashTransaction", null).Select("TransactionMonth");

        return result;

"result" is used normal 'GroupBy' query. I'm turning it to dynamic way as "resultList" where I am struggling. Any idea ?

I am using the following Dynamic Linq's Group by method:
public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values)
            if (source == null) throw new ArgumentNullException("source");
            if (keySelector == null) throw new ArgumentNullException("keySelector");
            if (elementSelector == null) throw new ArgumentNullException("elementSelector");
            LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values);
            LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values);
            return source.Provider.CreateQuery(
                    typeof(Queryable), "GroupBy",
                    new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
                    source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));