我想显示基于id和名称分组的记录数量,然后我想按显示的降序排序。
我的数据库结构如下: -
rowid id name viewedon viewedby
1 a1 reporter 23/07/2014 a@yahoo.com
2 a2 teacher 24/07/2014 test@gmail.com
3 a1 reporter 24/07/2014 wayne@zz.com
4 a2 teacher 24/07/2014 asdasd@zz.com
5 a1 reporter 24/07/2014 btttt@zz.com
6 a3 driver 26/02/2014 sfdf@yahoo.com
7 a4 operator 3/3/1991 asds@asd.com
8 a2 teacher 2/9/2011 tsdasd@gmail.com
9 a1 reporter 3/5/1992 asdas@fg.com
10 a4 operator 12/12/2005 asdss@a.com
11 a1 reporter 2/2/2002 as@as.com
12 a3 driver 10/7/2015 asdas@as.com
我的查询如下
var q=from j in context.item
group jobs by new { j.Id, j.Name } into gr
select new DAO<String>
{
Key = gr.Key.Name,
Count = gr.Count()
}).OrderByDescending(k => k.Count).Take(5).ToList();
这给出了如下正确的结果: -
key count
reporter 5
teacher 3
operator 2
driver 2
记者和老师的观点很高,所以他们的统计和秩序是合理的。
现在我对这个结果有疑问。运算符和驱动程序具有相同的视图计数,但由于驱动程序具有最新的视图日期,因此它应该在驱动程序之前。
所以我的结果应该是。
reporter 5
teacher 3
driver 2
operator 2
我的LINQ应该是什么来实现这个结果
答案 0 :(得分:1)
您需要选择要订购的列。你可以这样试试:
var q = (from j in context.item
group jobs by new { j.Id, j.Name } into gr
select new
{
Key = gr.Key.Name,
Count = gr.Count(),
LastViewedOn = gr.Max(o => o.viewedon)
}).OrderByDescending(k => k.Count).ThenByDescending(o => o.LastViewedOn)
.Select(o => new DAO<String>
{
Key = o.Key,
Count = o.Count
})
.Take(5).ToList();