使用c#在lambda表达式mvc中分组

时间:2014-09-10 14:42:51

标签: c# lambda group-by

我有一个lambda表达式,它返回正确的值,但我也想要分组项的总和,这里是我的查询

IEnumerable<CJTViewModel> objModel = (from q in db.CURRENT_JCT_TRANSACTION
where q.Job == job
group q by new { q.Cost_Code, q.Category } into g
select new CJTViewModel()
{
Job = job,
Category_E = g.Key.Category == "E" ? g.Sum(s => s.Amount) : 0,
Category_L = g.Key.Category == "L" ? g.Sum(s => s.Amount) : 0,
Category_LB = g.Key.Category == "LB" ? g.Sum(s => s.Amount) : 0,
Category_OH = g.Key.Category == "OH" ? g.Sum(s => s.Amount) : 0,
Cost_Code = g.Key.Cost_Code,
Category = g.Key.Category
}).ToList().OrderBy(x => x.Cost_Code );

它给我输出如下

Cost Code    Category      Amount
------------------------------------------------ 
1001               E        $100
1001               L        $200
1001               OH       $120
1002               L        $100
1002               LB       $100
1002               OH       $200
 

但我想要输出如下

Cost Code      Category     Amount
----------------------------------------------
1001              E          $100
1001              L          $200 
1001              OH         $120
                           -----------------
                             $420   
1002              L          $100
1002              LB         $100
1002              OH         $200
                        -----------------
                             $400 
 

任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:1)

您应该使用包含每个给定成本代码类别的总计和详细信息的模型。然后,您应按成本代码对交易进行分组,并选择这些模型。在每个成本代码组中,您应该按类别创建另一个分组,并选择包含给定成本代码/类别组摘要的模型。以下是匿名类型的示例:

var query = from t in db.CURRENT_JCT_TRANSACTION
            group t by t.Cost_Code into g
            orderby g.Key ascending
            select new
            {
                CostCode = g.Key,
                Total = g.Sum(t => t.Amount),
                Categories = 
                    from t in g
                    group t by t.Category into cg
                    select new
                    {
                        CostCode = g.Key,
                        Category = cg.Key,
                        Amount = cg.Sum(t => t.Amount)
                    }
            };

输出:

[
  {
    "CostCode": 1001,
    "Total": 420.0,
    "Categories": [ 
      { "CostCode": 1001, "Category": "E", "Amount": 100.0 },
      { "CostCode": 1001, "Category": "L", "Amount": 200.0 },
      { "CostCode": 1001, "Category": "OH", "Amount": 120.0 }
    ]
  },
  {
    "CostCode": 1002,
    "Total": 400.0,
    "Categories": [
      { "CostCode": 1002, "Category": "L", "Amount": 100.0 },
      { "CostCode": 1002, "Category": "LB", "Amount": 100.0 },
      { "CostCode": 1002, "Category": "OH", "Amount": 200.0 }
    ]
  }
]

答案 1 :(得分:0)

您需要一个完全不同的查询和视图模型来生成项目的第二个总和,因为就数据而言,它们是从不同的角度生成的。

你可以这样做:

        var groupedValues = viewModels.ToList()
            .GroupBy(model => model.CostCode,
                (i, models) => new {CostCode = i, Sum = models.Sum(model => model.Value)});

这将创建一个匿名类型,其中包含您可以在视图中访问和显示的成本代码和总和字段。

或者,您也可以声明一个新的viewmodel并填充:

public class CostCodeSummaryViewModel
{
    public int CostCode { get; set; }
    public int Total { get; set; }
}


        var groupedValues = viewModels.ToList()
            .GroupBy(model => model.CostCode,
                (i, models) => new CostCodeSummaryViewModel {CostCode = i, Total = models.Sum(model => model.Value)});