简体   繁体   English

Linq分组按日期排序然后是时间

[英]Linq Grouping Order By Date then Time

Good Evening, 晚上好,

I've managed to get my Linq query almost correct. 我设法让我的Linq查询几乎正确。 There is just one more issue I'm struggling to resolve. 还有一个问题我正在努力解决。

My query is 我的疑问是

var o =
  (from c in x
   group c by x.Date.Date into cc
   select new 
   { 
      Group = cc.Key.Date, 
      Items = cc.ToList(),
      ItemCount = cc.Count() 
   }).OrderByDescending(p => p.Group);

Now this query works fine. 现在这个查询工作正常。 It groups within a ListView by the date. 它按日期在ListView内进行分组。 x.Date is a DateTime field in my SQL Database. x.Date是我的SQL数据库中的DateTime字段。 Therefore I'm selecting x.Date.Date to Group by the actual Date of the DateTime field, as if it was just x.Date it would Group by the date and time. 因此,我按照DateTime字段的实际日期选择x.Date.Date为Group,就好像它只是x.Date它将按日期和时间分组。

My question is, how do I group by time so the newest time is at the top of the group? 我的问题是,我如何按时间分组,以便最新时间位于小组的最高位置?

Many thanks 非常感谢

Use the linq "ThenBy()" method: 使用linq“ThenBy()”方法:

var o = (from c in x group c by x.Date.Date into cc select new 
  { 
    Group = cc.Key.Date, 
    Items = cc.OrderByDescending(y=>y.Date).ToList(), 
    ItemCount = cc.Count() 
  })
  .OrderByDescending(p => p.Group)      

Items = cc.ToList()更改为Items = cc.OrderBy(c => c.[field_you_want_to_sort_by]).ToList()

var o =
  (from c in x
   group c by c.Date.Date into cc
   select new 
   { 
      Group = cc.Key.Date, 
      Items = cc.OrderByDescending(a=>a.Date.Time).ToList(),
      ItemCount = cc.Count() 
   }).OrderByDescending(p => p.Group);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM