Linq以降序获取不同的行列表

时间:2016-06-28 10:41:46

标签: entity-framework linq

考虑以下记录

  Id            F1            F2     f3         Date 
 -------------------------------------------------
   1           1800          1990     19      2016-06-27 09:24:25.550
   2           1181          1991     19      2016-06-27 09:25:15.243
   3           1919          2000     19      2016-06-27 11:04:27.807
   4           1920          2000     19      2016-06-27 13:04:27.807
   5           1800          2001     19      2016-06-28 09:24:25.550
   6           1181          2002     19      2016-06-28 09:25:15.243 
   7           1919          2010     19      2016-06-28 11:04:27.807

我希望Groupby f1按日期降序排序 Desirder输出

      Id            F1           F2    f3         Date 
     -------------------------------------------------
      7           1919          2010     19      2016-06-28 11:04:27.807    
      6           1181          2002     19      2016-06-28 09:25:15.243        
      5           1800          2001     19      2016-06-28 09:24:25.550
      4           1920          2000     19      2016-06-27 13:04:27.807

我试过

    DateTime EndDate=DateTime.Now.AddDays(-1);
     var result = (from opt in db.Output
                                     where opt.f3==19 && opt.Date > EndDate
                                     orderby opt.Date descending
                                     select new 
                                     {
                                         Id= opt.Id,
                                         F1=opt.F1,
                                         F2=opt.F2,
                                         F3=opt.F3,
                                         Date=opt.Date
                                     }).GroupBy(x => x.F1).Select(s => s.OrderBy(o => o.F2).FirstOrDefault()).OrderByDescending(x => x.Date).ToList();

我的输出为

 Id            F1            F2     f3         Date 
 -------------------------------------------------
   1           1800          1990     19      2016-06-27 09:24:25.550
   2           1181          1991     19      2016-06-27 09:25:15.243
   3           1919          2000     19      2016-06-27 11:04:27.807
   4           1920          2000     19      2016-06-27 13:04:27.807

我的代码出了什么问题。

3 个答案:

答案 0 :(得分:0)

如果我理解正确,你想要每组的最新项目:

db.Output.GroupBy(opt => opt.F1).
          Select(group => group.OrderByDescending(opt => opt.Date).First()).
          OrderBy(opt => opt.ID);

由于内部排序,我不确定SQL的转换是否有效。

GroupBy preserves order以来,您可以通过以下方式解决此问题:

db.Output.OrderByDescending(opt => opt.Date).
          GroupBy(opt => opt.F1).
          Select(group => group.First().
          OrderBy(opt => opt.ID);

答案 1 :(得分:0)

问题出在s.OrderBy(o => o.F2).FirstOrDefault()。这里的排序应该在Date

为什么您的代码不起作用:

//creates group
.GroupBy(x => x.F1)
//Order by F1 and take first - *Here the record with latest date is eliminated
.Select(s => s.OrderBy(o => o.F2).FirstOrDefault())
//This order by desc is of no use as we already have only 1 rec from each group
.OrderByDescending(x => x.Date).ToList();
var result = db.Output
              .Where(opt => opt.f3==19 && opt.Date > EndDate)
              .OrderByDescending(o => o.Date)
              .GroupBy(x => x.F1)
              .Select(s => s.FirstOrDefault())
              .ToList();

var result = db.Output
              .Where(opt => opt.f3==19 && opt.Date > EndDate)
              .OrderBy(o1=>o1.F2)
              .ThenByDescending(o => o.Date)
              .GroupBy(x => x.F1)
              .Select(s => s.FirstOrDefault())
              .ToList();

答案 2 :(得分:0)

使用多列组

 DateTime EndDate=DateTime.Now.AddDays(-1);
         var result = (from opt in db.Output
                                         where opt.f3==19 && opt.Date > EndDate
                                         orderby opt.Date descending
                                         select new 
                                         {
                                             Id= opt.Id,
                                             F1=opt.F1,
                                             F2=opt.F2,
                                             F3=opt.F3,
                                             Date=opt.Date
                                         }).GroupBy(x => new{x.Date, x.F1}).Select(s => s.OrderBy(o => o.F2).FirstOrDefault()).OrderByDescending(x => x.Date).ToList();