Linq 获取最大日期,return 具有最大日期的记录列表

Linq get max date, return list of records with max date

我想检索给定年份的记录列表,class 的最新生效日期。

按 Class、年份、范围最小值、范围最大值分组

Id   Class...Year...EffectiveDate...Value...RangeMin...RangeMax

1.  A.......2019....2019/1/1.........850......1.........100 

2.  A.......2019....2019/1/15........840......1.........100  

3.  A.......2019....2019/2/1.........550......101.......200  

4.  B.......2019....2019/1/5.........540......1.........100  

5.  B.......2020....2019/1/5.........650......1.........100  

6.  B.......2020....2019/5/1.........670......101.......200  

7.  B.......2020....2019/5/2.........680......101.......200

因此,如果我要查询 class A 和 2019 年到 return 的所有记录,则行列表:2,3

如果我要查询 class B 和 2020 年的所有记录到 return 行列表:5,7

var recordsInDb = (from record in context.records where record.Year == year & record.Class == class_ select record).ToList();

到目前为止,我已经能够获得给定年份 class 的所有记录的列表。 我知道我可以通过降低生效日期来添加订单。然而,return 仍然是所有记录,而不仅仅是那些具有最长生效日期的记录。

尝试以下操作:

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Class", typeof(string));
            dt.Columns.Add("Year", typeof(int));
            dt.Columns.Add("EffectiveDate", typeof(DateTime));
            dt.Columns.Add("Value", typeof(int));
            dt.Columns.Add("RangeMin", typeof(int));
            dt.Columns.Add("RangeMax", typeof(int));

            dt.Rows.Add(new object[] {1, "A", 2019, DateTime.Parse("2019/1/1"), 850,1, 100}); 
            dt.Rows.Add(new object[] {2, "A", 2019, DateTime.Parse("2019/1/15"), 840,1, 100}); 
            dt.Rows.Add(new object[] {3, "A", 2019, DateTime.Parse("2019/2/1"), 550,101, 200}); 
            dt.Rows.Add(new object[] {4, "B", 2019, DateTime.Parse("2019/1/5"), 540,1, 100}); 
            dt.Rows.Add(new object[] {5, "B", 2020, DateTime.Parse("2019/1/5"), 650,1, 100}); 
            dt.Rows.Add(new object[] {6, "B", 2020, DateTime.Parse("2019/5/1"), 670,101, 200}); 
            dt.Rows.Add(new object[] {7, "B", 2020, DateTime.Parse("2019/5/2"), 680,101, 200});

            DataTable results = dt.AsEnumerable()
                .OrderByDescending(x => x.Field<int>("Year"))
                .ThenByDescending(x => x.Field<DateTime>("EffectiveDate"))
                .GroupBy(x => new { cl = x.Field<string>("Class"), month = new DateTime(x.Field<DateTime>("EffectiveDate").Year, x.Field<DateTime>("EffectiveDate").Month, 1) })
                .Select(x => x.FirstOrDefault())
                .CopyToDataTable();

您可以只按您的标准分组,然后 select 每组中的最新者。

使用查询语法:

var recordsInDb = (from record in context.records
                   where record.Year == year & record.Class == class_
                   group record by new { record.Year, record.Class, record.RangeMin, record.RangeMax } into rg
                   select (
                        from record in rg
                        orderby record.EffectiveDate
                        select record
                   ).Last()
                  )
                  .ToList();

我认为使用 Fluent/lambda 语法更容易理解,因为 Last 无论如何都需要它:

var ans = context.records.Where(r => r.Year == year && r.Class == class_)
                         .GroupBy(r => new { r.Year, r.Class, r.RangeMin, r.RangeMax })
                         .Select(rg => rg.OrderBy(r => r.EffectiveDate).Last())
                         .ToList();