子查询的动态linq语法

时间:2014-05-29 22:23:27

标签: c# sql linq linq-to-sql dynamic-linq

我想在Dynamic LINQ中有以下查询..我已经尝试了一些解决方案,但还没有成功。

 select
    SUM([Value1]) AS [Sum]
        ,[Dim1] AS [Primary], 
        [Dim2] AS [Secondary]
    from
    (
        SELECT 
          value1, dim1, dim2
        FROM [BudgetLine]
        WHERE [BudgetID] = 4
    ) as a
    GROUP BY [Dim1], [Dim2]

我当前的代码看起来像这样,但我需要重写它以给我上面的SQL。

    var query = (DatabaseConnection.DataMemoryContext.GetTable<BudgetLineEntity>().AsQueryable()
    .Where(String.Format("BudgetID={0}",filter.BudgetId))
    .GroupBy(String.Format("new({0},{1})",primaryDimension.Name,secondaryDimension.Name), "new(Value1)")
    .Select(String.Format("new (Key.{0} as Primary, Key.{1} as Secondary, Sum(Value1) as Sum)",primaryDimension.Name,secondaryDimension.Name)));

primaryDimension.Name和SecondaryDimension.Name包含要分组的列的名称。

1 个答案:

答案 0 :(得分:0)

考虑查询子查询上的数据库,然后将结果存储在一个数据表中,然后再计算出来。

SELECT 
value1, dim1, dim2
FROM [BudgetLine]
WHERE [BudgetID] = 4

MSDN article - Compute