我在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();
最佳答案 试试这个:
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();