如何在动态linq查询中逐个查询

时间:2014-01-15 10:16:17

标签: c# linq entity-framework-5 lambda

我正在努力在动态linq查询中编写'逐个查询'。

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)
                          }).ToList();

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


        return result;

    }

“result”使用正常的“GroupBy”查询。我把它变成动态的方式作为“resultList”,我正在努力。任何的想法 ?

我正在使用以下Dynamic Linq的方法:

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(
            Expression.Call(
                typeof(Queryable), "GroupBy",
                new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
                source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));
    }

0 个答案:

没有答案