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

时间:2019-11-04 20:54:35

标签: c# linq linq-to-entities

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

按类别,年份,RangeMin,RangeMax分组

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

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

使用查询语法:

MWE <- c("R text1 47 GDP -2.4 3.4", "Euro area but not UK CPI 3.7 6.4", "Argentina -2.4 3.4", "Euro area 3.7 6.4")
gsub("^(?:.*\\b(GDP|CPI)\\b|.*?([+-]?\\d))", "\\1\\2", MWE, perl=TRUE)
## => [1] "GDP -2.4 3.4" "CPI 3.7 6.4"  "-2.4 3.4"     "3.7 6.4" 

我认为使用Fluent / lambda语法会更容易一些,因为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(); 仍然需要它:

Last
相关问题