使用EF6 DbFunctions.TruncateTime比较DateTime时出错 [英] Error using EF6 DbFunctions.TruncateTime comparing DateTime
问题描述
我试图使用以下代码从数据范围中检索一些数据:
I`m trying to retrieve some data from a data range using the following code:
var rotas = db.X.Where(r => r.DataDaExecucao != null)
.Where(r => System.Data.Entity.DbFunctions.TruncateTime(r.Date.Value) >= System.Data.Entity.DbFunctions.TruncateTime(startDateTime))
.Where(r => System.Data.Entity.DbFunctions.TruncateTime(r.Date.Value) < System.Data.Entity.DbFunctions.TruncateTime(endDateTime))
.Join(db.T, r => r.Id, t => t.X_Id.Value,
(r, t) => new
{
id = r.Id,
start = r.Date.Value.ToString("s"),
end = r.Date.Value.AddDays(1).ToString("s"),
title = t.Z.Name,
allday = false
}).ToList();
Date属性为Nullable< DateTime>。
"Date" properties are Nullable< DateTime>.
我收到以下错误信息:
LINQ到实体不会识别方法'System.String ToString(System.String)'方法,并且此方法不能转换为存储表达式。
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
异常详细信息:系统.NotSupportedException:LINQ to Entities不能识别方法'System.String ToString(System.String)'方法,并且此方法不能转换为存储表达式。
Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
此外,我没有在我的csproj中引用System.Data.Entity.dll程序集。
Also, I don`t have the System.Data.Entity.dll assembly referenced in my csproj.
想法?
提前谢谢。
推荐答案
您可以使用< a href =http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions%28v=vs.110%29.aspx =nofollow> SqlFunctions 在那里它也将生成sql查询。
You can change the anonymous type using SqlFunctions where it will also generate the sql query.
在你的情况下,我们e ToString(s)
,意思是要获取可以由 SqlFunctions :: DatePart $ c替换的第二个日期部分$ C>。而
Date :: AddDays
可以替换为 SqlFunctions :: DateAdd
。
In your case you use ToString("s")
, meaning you want to get the date part of second that can be replaced by SqlFunctions::DatePart
. And Date::AddDays
can be replaced by SqlFunctions::DateAdd
.
new
{
id = r.Id,
start = SqlFunctions.DatePart("s", r.Date),
end = SqlFunctions.DatePart("s", SqlFunctions.DateAdd("d", 1, r.Date)),
title = t.Z.Name,
allday = false
}
这篇关于使用EF6 DbFunctions.TruncateTime比较DateTime时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!