将包含top,count,group和order的SQL转换为LINQ(2个实体) [英] Converting SQL containing top, count, group and order to LINQ (2 Entities)
本文介绍了将包含top,count,group和order的SQL转换为LINQ(2个实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一些LINQ查询仍然困扰着我.
Some LINQ queries still puzzle me.
对于包含两个列(页面"和日期")的命中"表,我想在定义的时间范围内找到行数最多的页面.
for a table 'Hits' containing two columns, 'Page' and 'Date', I want to find the most Pages with the most rows in a defined slice of time.
在SQL中,我将使用此代码:
In SQL I would use this:
SELECT TOP 10
[Page]
,COUNT([Page]) as Number
FROM dbo.[Hits]
WHERE [Date] >= CONVERT(datetime,'14 Jan 2009')
AND [Date] < CONVERT(datetime,'15 Jan 2009')
Group BY [Page]
Order by Number DESC
在LINQ中,我不知道该如何处理,有人可以在这里帮助我吗?我试图使用linqer对其进行转换,但是它只显示了此表达式的错误.
In LINQ I got no idea how to approach this, can anyone help me here? I tried to convert it using linqer, but it just shows an error for this expression.
推荐答案
类似的方法应该起作用:
Something like this should work:
(from p in DataContext.Hits
where (p.Date >= minDate) && (p.Date < maxDate)
group p by p.Page into g
select new { Page = g.Key, Number = g.Count() }).OrderByDescending(x => x.Number).Take(10);
这篇关于将包含top,count,group和order的SQL转换为LINQ(2个实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文