Art*_*has 24 c# linq entity-framework
mycode的:
//get data
var myData = from log in db.OperationLogs
group log by log.CreateTime.Date into g
orderby g.Key
select new { CreateTime = g.Key, Count = g.Count() };
Run Code Online (Sandbox Code Playgroud)
这段代码会抛出像实体框架一样的异常,不支持get Date操作.因为log.createtime都有日期和时间,我想按日期分组,我该怎么做
Mar*_*zek 49
使用EntityFunctions.TruncateTime
方法(Nullable<DateTime>
).它将TRUNCATETIME()
在生成的SQL查询中转换为TSQL函数,它可以满足您的需求:
返回表达式,截断时间值.
所以你的代码应该如下:
//get data
var myData = from log in db.OperationLogs
group log by EntityFunctions.TruncateTime(log.CreateTime) into g
orderby g.Key
select new { CreateTime = g.Key, Count = g.Count() };
Run Code Online (Sandbox Code Playgroud)
对于更高版本的实体框架,这是一种更简单的方法。
var query = Data
.GroupBy(o => new { EventDate = o.EventDate.Date })
.Select(s => new SalesData()
{
EventDate = s.Key.EventDate,
Amount = s.Sum(o => o.Amount),
Qty = s.Sum(o => o.Qty),
RefundAmount = s.Sum(o => o.RefundAmount),
RefundQty = s.Sum(o => o.RefundQty),
})
.OrderBy(o => o.EventDate)
.ToList();
Run Code Online (Sandbox Code Playgroud)
返回查询;
归档时间: |
|
查看次数: |
9876 次 |
最近记录: |