C#Linq嵌套了GroupBy和Sum

时间:2016-03-03 18:17:10

标签: c# linq lambda

我试图翻译我过去几天写给Linq的查询,但我似乎无法使其发挥作用。这是我试图翻译的查询:

SELECT
    hsd.CoveragePeriodBeginDate,
    RateTotal           = SUM(hsd.Rate),
    ReimbursementTotal  = SUM(hsd.TotalReimbursement),
    AdjustmentsTotal    = SUM(hsd.Adjustments)
FROM
    (   SELECT 
            CoveragePeriodBeginDate,
            PaidDate,
            Rate                = TotalClaimCharge, 
            TotalReimbursement  = ReimbursementAmount,
            Adjustments         = SUM(BaseRateChangeAmount)
        FROM
            dbo.HsdMonthlyCapitatation
        WHERE
            MemberID = 12345678
        GROUP BY
            CoveragePeriodBeginDate,
            PaidDate,
            TotalClaimCharge,
            ReimbursementAmount
    )   hsd 
GROUP BY
    hsd.CoveragePeriodBeginDate
ORDER BY 
    hsd.CoveragePeriodBeginDate

我需要做的是将其翻译成Linq。我尝试了很多不同的方法,但似乎无法使其正常工作。它似乎总是聚合太多。

这里是我最接近的人。

var rawCapData = db.HsdMonthlyCapitations.Where(x => x.MemberID == memberID)
    .Select(x => new {
                        CoveragePeriod = x.CoveragePeriodBeginDate,
                        TotalCharge = x.TotalClaimCharge,
                        Reimbursement = x.ReimbursementAmount,
                        PaidDate = x.PaidDate,
                        Adjust = x.BaseRateChangeAmount
                    })
    .GroupBy(x => new {
                        CoverageDate = x.CoveragePeriod,
                        Paid = x.PaidDate,
                        Rate = x.TotalCharge,
                        Reimburse = x.Reimbursement
                      })
    .GroupBy(x => new {
                        Coverage = x.Key.CoverageDate,
                        DhsRate = x.Sum(y => y.TotalCharge),
                        ReimbursementTotal = x.Sum(y => y.Reimbursement),
                        Adjustments = x.Sum(y => y.Adjust)
                      })
    .Select(x => new {
                        CapMonthYear = x.Key.Coverage,
                        DhsRate = x.Key.DhsRate,
                        TotalReimbursement = x.Key.ReimbursementTotal,
                        AdjustmentsTotal = x.Key.Adjustments
                     });   

我应该说我已经让它工作了,但我觉得它相当笨拙,并且混合了常规的LINQ和lambda表达式,我宁愿用lambda表达式编写所有代码,如果可能的话。这是我开始工作的代码:

var rawCapitationData = from capitation
                        in db.HsdMonthlyCapitations
                        where capitation.MemberID == memberID
                        group capitation by new 
                             { 
                                capitation.CoveragePeriodBeginDate,
                                capitation.TotalClaimCharge,
                                capitation.ReimbursementAmount,
                                capitation.PaidDate
                             } into cap
                        select new {
                                      CapitationMonthYear = cap.Key.CoveragePeriodBeginDate,
                                      TotalReimbursement = cap.Key.TotalClaimCharge,
                                      DhsCapitationAmount = cap.Key.ReimbursementAmount,
                                      PaidDate = cap.Key.PaidDate,
                                      DhsAdjustments = cap.Sum(x => x.BaseRateChangeAmount)
                                   };

var capitationData = rawCapitationData.GroupBy(cap => cap.CapitationMonthYear)
      .Select(data => new {
                           CapitationDate = data.Key,
                           TotalReimbursement = data.Sum(x => x.TotalReimbursement),
                           DhsCapitationAmount = data.Sum(x => x.DhsCapitationAmount),
                           DhsAdjustments = data.Sum(x => x.DhsAdjustments)
                          });

我的偏好是在一个声明中完成所有这些。它甚至可能吗?我觉得我与lambda表达式很接近,但我知道我错过了什么。

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

不确定你想要实现的目标,但我最终得到了这个:

return db.HsdMonthlyCapitations
    .Where(x => x.MemberID == memberID)
    .GroupBy(x => new {x.CoveragePeriodBeginDate, x.PaidDate, x.TotalClaimCharge, x.ReimbursementAmount})
    .Select(x => new
    {
        x.Key.CoveragePeriodBeginDate,
        x.Key.PaidDate,
        Rate = x.Key.TotalClaimCharge,
        TotalReimbursement = x.Key.ReimbursementAmount,
        Adjustments = x.Sum(m => m.BaseRateChangeAmount)
    })
    .GroupBy(x => x.CoveragePeriodBeginDate)
    .Select(x => new
    {
        CoveragePeriodBeginDate = x.Key,
        RateTotal = x.Sum(m => m.Rate),
        ReimbursementTotal = x.Sum(m => m.TotalReimbursement),
        AdjustmentsTotal = x.Sum(m => m.Adjustments),
    })
    .OrderBy(x => x.CoveragePeriodBeginDate);