【问题标题】:c# linq filter group by max composite keyc# linq filter group by max Composite key
【发布时间】:2021-02-15 18:08:59
【问题描述】:

我有一个 linq 查询:

 var reports = await dbContex.ShoppingListPatientReports
                             .Where(report => patientIds.Contains(report.PatientId))
                             .GroupBy(report => new { report.PatientId, RecentDate = DbFunctions.TruncateTime(report.DateCreated) })
                             .OrderByDescending(g => g.Key)
                             .ToListAsync();

它返回按复合键(PatientId、RecentDate)降序排序的组。 键:

10004, 2021-02-03
10004, 2021-01-01
10004, 2021-02-02
10002, 2021-01-05
10002, 2021-01-06

我能否以某种方式只获取具有最大键的组(PatientId、RecentDate) 即最近日期的组(在本例中,结果应该是两个组):

10004, 2021-02-03
10002, 2021-01-06 

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案1】:

    您需要在GroupBy 之后添加Where

    .Where(grp =>
        grp.OrderByDecending(x => x.Key.RecentDate).First().PatientId == grp.Key.PatientId)
    

    完整查询:

    var reports = await dbContex.ShoppingListPatientReports
        .Where(report => patientIds.Contains(report.PatientId))
        .GroupBy(report => new
        {
            report.PatientId,
            RecentDate = DbFunctions.TruncateTime(report.DateCreated)
        })
        .Where(grp =>
            grp.OrderByDecending(x => x.Key.RecentDate).First().PatientId == grp.Key.PatientId)
        .OrderByDescending(g => g.Key)
        .ToListAsync();
    

    【讨论】:

    • 这个查询真的有效吗?哪个 EF 版本?
    • @SvyatoslavDanyliv 未经测试;可能翻译不出来..
    • 所以,在你身边测试类似的查询,你会感到惊讶。这里的每个人都知道 LINQ to Objects,但是 LINQ to Entities 非常有限,尤其是在分组方面。
    【解决方案2】:

    也许它对你有用。它适用于元素列表,但我从未在 EF 中尝试过。

     var reports = new List<ShopingPatientReport>() { ... }
                                 .Where(report => patientIds.Contains(report.PatientId))
                                 .GroupBy(report => report.PatientId)
                                 .Select(report => report.GroupBy(x => DbFunctions.TruncateTime(report.DateCreated))
                                                         .OrderByDescending(f => f.Key).First()).OrderByDescending(g => g.Key)
                                 .ToList()
    

    【讨论】:

      【解决方案3】:

      这是一个适合我的查询:

      await dbContex.ShoppingListPatientReports
                     .Where(report => patientIds.Contains(report.PatientId))
                     .GroupBy(report => report.PatientId)
                     .Select(patientShoppingList =>
                              new
                                {
                                  patientId = patientShoppingList.Key,
                                                   //Grouping reports by created date and take group wiht the most recent date  
                                   reports = patientShoppingList.GroupBy(report => DbFunctions.TruncateTime(report.DateCreated))
                                                                                .OrderByDescending(group => group.Key)
                                                                                .FirstOrDefault()
                                })
         .SelectMany(g => g.reports.Select(r => r))
         .ToListAsync();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多