Converting SQL containing top, count, group and order to LINQ (2 Entities)

后端 未结 2 1151
春和景丽
春和景丽 2020-12-21 01:08

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

相关标签:
2条回答
  • 2020-12-21 01:39
    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);
    
    0 讨论(0)
  • 2020-12-21 01:45

    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);
    
    0 讨论(0)
提交回复
热议问题