我只想在Linq中执行此SQL。但是如何?
Select Id, sum(Count)
from dbo.BThread
group by Id
order by sum(Count) desc
我尝试了这个并且失败了:结果只给出了一行。我期待比这更多的行。我需要总结许多ID。
var List = (from tbBThread in context.BThread.Take(10)
group tbBThread by tbBThread.Id into bTmp
orderby bTmp.Sum(x => x.Count)
select new BViewModel
{
Id = bTmp.Key,
Id = bTmp.First().Id,
Count = bTmp.Sum(x => x.Count)
}).ToList();
答案 0 :(得分:0)
Take(10)
应该在查询的末尾。
var List = (from tbBThread in context.BThread
group tbBThread by tbBThread.Id into bTmp
orderby bTmp.Sum(x => x.Count)
select new BViewModel
{
Id = bTmp.Key,
Count = bTmp.Sum(x => x.Count)
}).Take(10).ToList();
否则,您只会查看context.BThread
的前10个元素,这些元素似乎具有相同的Id
,因此会进入同一组。
并且(正如Slai已在评论中所述)删除第二个Id = bTmp.First().Id,
。