LINQ-to-Entities Find DateTime difference
I'm trying to compare difference of two DateTimes in an L2E query. I know that the translation of DateTime methods to Entity Framework is really problematic, and I need a precise comparison in minutes. I tried to compare the Ticks
of DateTime
but it wasn't supported. I can test for the same day, hour, and minute explicitly (using the methods in the Microsoft.VisualBasic.DateAndTime
class does this trick, and I don't know why regular DateTime still isn't supported yet), but it will be problematic for, say, midnight values, and I don't want to cope up with checking dates, then if they are different, check if the days are adjacent, and then the the hours/minutes, there must be a more convenient way. How do I compare the ticks (which is the most straightforward way to do this for me 开发者_如何学编程to check for, like 15 minutes of difference) in an L2E query?
You can use the SqlFunctions object: http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx I'm not sure if these are SQL Server specific or note...
HTH.
An alternative to find Datetime difference could be something like-
var p = (from ad in dcDistrict.ADULTs
where DateTime.Now.Subtract(ad.CREATED.Value).Minutes >= 15
select ad);
精彩评论