LINQ to Entities减去2个日期 [英] LINQ to Entities for subtracting 2 dates
问题描述
我正在使用LINQ与实体框架来确定两个日期之间的天数。它告诉我,它不承认Subtract在System.TimeSpan类
I am trying to determine the number of days between 2 dates using LINQ with Entity Framework. It is telling me that it does not recognize Subtract on the System.TimeSpan class
这是我的LINQ查询的哪个部分。
Here is my where portion of the LINQ query.
where ((DateTime.Now.Subtract(vid.CreatedDate).TotalDays < maxAgeInDays))
这是VS.NET调试器中收到的错误
Here is the error I receive in the VS.NET debugger
{ LINQ to Entities不能识别方法'System.TimeSpan Subtract(System.DateTime)'方法,并且这种方法不能被转换为存储表达式。}
{"LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression."}
我做错了事情,还是有更好的方式在实体框架中获得2 DateTimes之间的天数?
Am I doing something wrong or is there a better way to get the number of days between 2 DateTimes in the entity framework?
谢谢
Michael
thanks Michael
推荐答案
这是我如何工作
我定义了代表最早日期的datetime变量
I defined a datetime variable that represents the oldest date
DateTime oldestDate = DateTime.Now.Subtract(new TimeSpan(maxAgeInDays, 0, 0, 0, 0));
...
然后我修改了LINQ查询的哪个部分
then I modified the where portion of the LINQ query
where (vid.CreatedDate >= oldestDate )
像一个魅力一样 - 感谢Micah让我思考表情树
worked like a charm - thanks Micah for getting me to think about the expression tree
这篇关于LINQ to Entities减去2个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!