我在SQL中编写查询,通过clickCount列
选择前12个访问类别SELECT top(12) c.Name
FROM Category c
GROUP BY c.Name
ORDER BY SUM(ClickCount) DESC
我需要在linq中实现。到目前为止,我写了这个,但结果并不像预期的那样
var query = _categoryRepository.Queryable()
.GroupBy(r => r.Name)
.SelectMany(g => g.OrderBy(r => r.ClickCount))
.Take(12).ToList();
答案 0 :(得分:2)
试试这个:
var query = (from item in _categoryRepository.Queryable()
group item by item.Name into sub
orderby sub.Sum(x => x.ClickCount) descending
select sub).Take(12).ToList();