seb*_*mez 4 linq linq-to-entities entity-framework
用简单的英语很难问这个问题,所以我会展示我想要做的事情.
这是我的SQL代码:
select top 100 [Name], COUNT([Name]) as total from ActivityLog
where [Timestamp] between '2010-10-28' and '2010-10-29 17:00'
group by [Name]
order by total desc
Run Code Online (Sandbox Code Playgroud)
我需要在LinQ中写一下.到目前为止,我有以下内容:
var groups = from ActivityLog log in ctx.ActivityLog
where log.Timestamp > dateFrom
where log.Timestamp <= dateTo
group log by log.Name;
Run Code Online (Sandbox Code Playgroud)
但我没有COUNT(*)
列来排序:(
我担心我对fluent语法(与查询语法相反)更为舒服,但这里有一个可能的LINQ答案:
ctx.ActivityLog
.Where(x => x.TimeStamp > dateFrom && x.TimeStamp <= dateTo)
.GroupBy(x => x.Name)
.Select(x => new { Name = x.Key, Total = x.Count() })
.OrderByDescending(x => x.Total)
.Take(100)
Run Code Online (Sandbox Code Playgroud)
编辑:
好吧,我走出了我的舒适区,想出了一个查询语法版本,只是不要期望太多.我警告过你我上面的能力:
(from y in (
from x in (
from log in ActivityLog
where log.Timestamp > dateFrom
where log.Timestamp <= dateTo
group log by log.Name)
select new { Name = x.Key, Total = x.Count() })
orderby y.Total descending
select new { Name = y.Name, Total = y.Total }).Take(100)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2044 次 |
最近记录: |