以下代码EntityCommandCompilationException因注释行而抛出一个:
var datePartArg = "dd";
var minutesInStatePerSegment = await db.History_WorkPlaceStates
.Where(x => selector.StartTimeUtc <= x.Started && x.Ended < selector.EndTimeUtc)
.Select(x => new {
start = x.Started,
minutes = x.Minutes,
state = x.State,
})
.GroupBy(x => new {
//This causes an exception:
segment = SqlFunctions.DateDiff(datePartArg, selector.StartTimeUtc, x.start),
state = x.state,
})
.Select(x => new {
state = x.Key.state,
segment = x.Key.segment,
minutes = x.Sum(y => y.minutes),
}).ToListAsync();
发生这种情况是因为DateDiff在 SQL Server 中只能将文字字符串用作其第一个参数,而不能使用变量。实体框架在 SQL 中生成一个变量,因此我们得到了异常。
有没有办法解决这个问题?
相关分类