Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework Linq以降序获得不同的行列表_Entity Framework_Linq - Fatal编程技术网

Entity framework Linq以降序获得不同的行列表

Entity framework Linq以降序获得不同的行列表,entity-framework,linq,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.24

考虑以下记录

  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
我想按f1分组,按日期降序排序 期望输出

      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

我的代码有什么问题。

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

db.Output.GroupBy(opt => opt.F1).
          Select(group => group.OrderByDescending(opt => opt.Date).First()).
          OrderBy(opt => opt.ID);
虽然由于内部顺序的原因,我不确定转换为SQL是否有效

现在,您可以通过以下方法解决此问题:

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

问题出在
s.OrderBy(o=>o.F2).FirstOrDefault()
中。此处订购日期应为
日期

代码不起作用的原因:

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


使用多列组

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

我也尝试过这段代码,但输出是一样的。你得到opt#1了吗?如果是这样的话,这很奇怪,日期的类型是什么?日期是日期时间类型。
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();
 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();