开发者

compare the dates with linq

开发者 https://www.devze.com 2022-12-21 09:10 出处:网络
I have this code: i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null && ((DateTime)p.Giorno).Date == ((DateTime)i.Data).Date &&

I have this code:

   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);

If i launch i开发者_开发知识库t i obtain this error:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

how can i compare the datetimes field without the hours part

thanks


Well this is certainly not the most efficient way to do it, but you can try something like:

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);

Depending on the requirements I would probably implement something cleaner and faster but it would depend on how the dates are stored in your database.


Check out this question it looks like the syntax error may be in your SQL, not in your LINQ. The asker in that question had written:

SQL ------
Select
 EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
SQL ------

When they should have written:

SQL ------
Select
 EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn <= GETDATE() //Today
SQL -


solution for your problem will be the "SqlFunctions class and DateDiff" http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号