晚安,
我设法让我的Linq查询几乎正确。还有一个问题我正在努力解决。
我的查询是
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);
现在这个查询工作正常。它按日期在ListView内进行分组。 x.Date是我的SQL数据库中的DateTime字段。因此,我按照DateTime字段的实际日期选择x.Date.Date为Group,好像它只是x.Date它将按日期和时间分组。
我的问题是,我如何按时间分组,以便最新时间位于小组的最顶层?
非常感谢
答案 0 :(得分:4)
使用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)
答案 1 :(得分:2)
将Items = cc.ToList()
更改为Items = cc.OrderBy(c => c.[field_you_want_to_sort_by]).ToList()
答案 2 :(得分:1)
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);