我有这段代码:
i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null &&
((DateTime)p.Giorno).Date == ((DateTime)i.Data).Date &&
p.MissioneID == missioneID).Sum(p => p.Costo);
如果我启动它,我会收到此错误:
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
如何比较没有小时部分的日期时间字段
感谢
答案 0 :(得分:1)
这当然不是最有效的方法,但您可以尝试以下方法:
i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null &&
(new DateTime((p.Giorno as DateTime).Year, (p.Giorno as DateTime).Month, (p.Giorno as DateTime).Day) == (new DateTime((i.Data as DateTime).Year, (i.Data as DateTime).Month, (i.Data as DateTime).Day) &&
p.MissioneID == missioneID).Sum(p => p.Costo);
根据要求,我可能会实现更清洁,更快速的东西,但这取决于日期在数据库中的存储方式。
答案 1 :(得分:0)
看看这个question看起来语法错误可能在你的SQL中,而不是在你的LINQ中。该问题的提问者写道:
SQL ------
Select
EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
SQL ------
他们应该写的时候:
SQL ------
Select
EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn <= GETDATE() //Today
SQL -
答案 2 :(得分:0)
您的问题的解决方案将是“SqlFunctions类和DateDiff”http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx