我正在尝试编写一个 LINQ 查询,这对我来说很复杂,因为我是 LINQ 的新手。
我有一个如下表...
UserId | CompanyId | ProblemDescription | CreatedTime | TimeSpentMins
-----------------------------------------------------------------------
1 | 95 | Sysmtem is crashed | 2016-01-01 15:23 | 25
1 | 95 | Total is incorrect | 2016-01-01 15:45 | 45
我想编写一个 LINQ 查询来完成下面的工作。 CreatedTime 有日期和时间,但我只想按日期对它进行分组。
SELECT UserId, CompanyId,CreateTime Sum(TimeSpentMins)
FROM TransactionLogs
GROUP BY UserId, CompanyId, Convert(DATE,CreatedTime)
如何编写此 LINQ?我想把我的代码放在下面,但我一无所获:(
请您参考如下方法:
只需使用 GroupBy扩展方法及使用EntityFunctions.TruncateTime仅获取日期部分的方法:-
var result = db.TransactionLogs
.GroupBy(x => new
{
CreateTime = EntityFunctions.TruncateTime(x.CreatedTime),
UserId,
CompanyId
})
.Select(x => new
{
UserId = x.Key.UserId,
CompanyId = x.Key.CompanyId,
CreateTime = x.Key.CreateTime,
TotalTimeSpentMins = x.Sum(z => z.TimeSpentMins)
});
