在 SQL CE 数据库上的实体框架中使用 DateDiff
我有一个方法应该返回带有计算列的匿名对象列表,如下所示:
var tomorrow = DateTime.Today.AddDays(1);
return from t in this.Events
where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
select new
{
Client = t.Activity.Project.Customer.Name,
Project = t.Activity.Project.Name,
Task = t.Activity.Task.Name,
Rate = t.Activity.Rate.Name,
StartTime = t.StartTime,
EndTime = t.EndTime.Value,
Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime.Value) / 60),
Description = t.Activity.Description
};
不幸的是,我从 DateDiff 函数中收到以下错误:
指定的方法 'System.Nullable1[System.Int32] DateDiff类型“System.Data.Objects.SqlClient.SqlFunctions”上的 (System.String, System.Nullable
1[System.DateTime], System.Nullable`1[System.DateTime])' 无法转换为LINQ to Entities 存储表达式。
有什么想法我可能在这里做错了吗?
编辑:我还尝试了这里提到的EntityFunctions类,但这并没有那么有效。
Minutes = EntityFunctions.DiffMinutes(t.EndTime, t.StartTime),
I have a method which should return a list of anonymous objects with a calculated column like this:
var tomorrow = DateTime.Today.AddDays(1);
return from t in this.Events
where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
select new
{
Client = t.Activity.Project.Customer.Name,
Project = t.Activity.Project.Name,
Task = t.Activity.Task.Name,
Rate = t.Activity.Rate.Name,
StartTime = t.StartTime,
EndTime = t.EndTime.Value,
Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime.Value) / 60),
Description = t.Activity.Description
};
Unfortunately I get the following error from the DateDiff function:
The specified method 'System.Nullable1[System.Int32] DateDiff(System.String, System.Nullable
1[System.DateTime], System.Nullable`1[System.DateTime])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.
Any ideas what I could have done wrong here?
EDIT: I also tried the EntityFunctions class mentioned here, but that did not work as well.
Minutes = EntityFunctions.DiffMinutes(t.EndTime, t.StartTime),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[编辑]
不支持 SQL CE。
引发 DbArithmeticExpression 异常
因此,我认为您必须分两步完成。抓取所需的数据,然后计算内存中的时间差。
[Edit]
is not supported SQL CE.
Throws an DbArithmeticExpression Exception
So, I think you'll have to do it in two steps. Grab the data you need, then calculate the time difference in memory.
您要求 EF 提供程序处理 SQL 中的 DATEDIFF,我认为这在任何版本的 SQL 中都是不可能的。使用本机日期时间函数:
You're asking the EF provider to handle the DATEDIFF in SQL, which I don't think is possible in any version of SQL. Use native DateTime functions:
两种可能性,从 EndTime 中删除 Value:
或使用 DateDiffHour:
Two possibilities, remove Value from EndTime:
Or use DateDiffHour: