一些LINQ查询仍然困扰着我.
对于包含两列,'Page'和'Date'的表'Hits',我想找到在定义的时间片段中具有最多行的页面.
在SQL中我会使用这个:
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转换它,但它只显示此表达式的错误.
这样的事情应该有效:
(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);
var top10hits = objectContext.Hits .Where(h => minDate <= h.Date && h.Date < maxDate) .GroupBy(h => h.Page) .Select(g => new { Page = g.Key, Number = g.Count() }) .OrderByDescending(x => x.Number) .Take(10);