将日期与linq进行比较

时间:2010-03-01 15:02:18

标签: linq entity-framework date

我有这段代码:

   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.

如何比较没有小时部分的日期时间字段

感谢

3 个答案:

答案 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

相关问题