在SQL Server数据库中,我有一个记录条目的表格 -
我需要在指定的时间间隔(30分钟)内获取日志发生情况。
例如:
时间戳
和描述
。我需要在指定的时间间隔(30分钟)内获取日志发生情况。
例如:
01.01.2015 00:00 - 100
01.01.2015 00:30 - 200
01.01.2015 01:00 - 2
and so on...
What I have:
var logs = session.Query<Log>;
//... other operations on logs queryable
var intervalInMinutes = 30;
var logsFrequency = logs
.GroupBy(l => new
{
Year = l.LogTimestamp.Year,
Month = l.LogTimestamp.Month,
Day = l.LogTimestamp.Day,
Hour = l.LogTimestamp.Hour,
Minute = (l.LogTimestamp.Minute / intervalInMinutes) * intervalInMinutes,
})
.Select(g => new LogsOccurence()
{
StartingDatetime = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day, g.Key.Hour, g.Key.Minute, 0),
Frequency = g.Count()
})
.ToList();
以下查询失败:
发生类型为“NHibernate.Exceptions.GenericADOException”的异常,但未在用户代码中处理
其他信息:无法执行查询
内部异常:因为它不包含在聚合函数或GROUP BY子句中,所以列'dbo.Log.LogTimestamp'在选择列表中无效。
生成的select语句是:
select
datepart(year, Log0_.LogTimestamp) as col_0_0_
, datepart(month, Log0_.LogTimestamp) as col_1_0_
, datepart(day, Log0_.LogTimestamp) as col_2_0_
, datepart(hour, Log0_.LogTimestamp) as col_3_0_
, datepart(minute, Log0_.LogTimestamp) as col_4_0_
, cast(count(*) as INT) as col_5_0_
from
dbo.[Log] Log0_
group by
datepart(year, Log0_.LogTimestamp)
, datepart(month, Log0_.LogTimestamp)
, datepart(day, Log0_.LogTimestamp)
, datepart(hour, Log0_.LogTimestamp)
, datepart(minute, Log0_.LogTimestamp)/@p0*@p1
Name:p1 - Value:30 Name:p2 - Value:30
Operator precedence / order is wrong in
groupby
clausedatepart(minute, Log0_.LogTimestamp) / @p0 * @p1
Select part for minutes is different than group part for minutes - select fails
我的Linq有什么问题,如何编写正确的代码?
我正在使用NHibernate build 4.0.4.GA。