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

问题描述 投票:-2回答:2

我想检索给定Year,最新生效日期的类的记录列表。

按类别,年份,范围最小值,范围最大值分组

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

因此,如果我要查询所有A类和2019年的记录以返回行列表:2,3

如果我查询所有属于B类和2020年的记录以返回行列表:5,7

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

到目前为止,我已经能够获得给定年级的所有记录的列表。我知道我可以通过降低生效日期来添加订单。但这仍然会返回所有记录,而不仅仅是有效日期最高的记录。

c# linq linq-to-entities
2个回答
0
投票

您可以按照条件分组,然后从每个分组中选择最新的。

使用查询语法:

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();

0
投票

尝试以下内容:

            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();
© www.soinside.com 2019 - 2024. All rights reserved.