C# LINQ to Entities 无法识别“System.TimeSpan Subtract(System.DateTime)”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15264319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method
提问by Ragesh S
I try to select records in database in 60 days 30 days 20 days differents in current date.
我尝试在当前日期的 60 天 30 天 20 天中选择数据库中的记录。
Please see this query in below.
请在下面查看此查询。
var uploads = (
from files in _fileuploadRepository.Table
join product in _productRepository.Table on files.Event equals product.Id
where
(
product.EventDate != null &&
(product.EventDate.Subtract(DateTime.Now).Days <= 60 && product.EventDate.Subtract(DateTime.Now).Days >= 60) ||
(product.EventDate.Subtract(DateTime.Now).Days <= 30 && product.EventDate.Subtract(DateTime.Now).Days >= 30) ||
(product.EventDate.Subtract(DateTime.Now).Days <= 20 && product.EventDate.Subtract(DateTime.Now).Days >= 20))
&&
files.IsSkiped == false
select files;
).ToList();
But a error occurred this query.
但是这个查询出错了。
I am clueless. Please Help.
我一窍不通。请帮忙。
采纳答案by Jon Skeet
The simplest approach is to work out the bounds beforeyou perform the query:
最简单的方法是在执行查询之前计算边界:
// Only evaluate DateTime.Now once for consistency. You might want DateTime.Today instead.
DateTime now = DateTime.Now;
DateTime nowPlus60Days = now.AddDays(60);
DateTime nowPlus30Days = now.AddDays(30);
DateTime nowPlus20Days = now.AddDays(20);
var query = ...
where product.EventDate <= nowPlus60Days
...
Note that your current query doesn't even really make sense, as each "or"'d clause is stating that the given computation is both less than or equal to a value andgreater than or equal to the same value. If you want simple "equal to" then use that. If not, it's not clear what you aretrying to do.
请注意,您当前的查询甚至没有任何意义,因为每个“或”d 子句都说明给定的计算既小于或等于一个值,又大于或等于相同的值。如果您想要简单的“等于”,请使用它。如果没有,目前还不清楚是什么你正在尝试做的。
If you're trying to bucket the values into "less than 20", "20-30", "30-60", "more than 60" you'll need to use grouping of some form.
如果您尝试将值分为“小于 20”、“20-30”、“30-60”、“超过 60”,则需要使用某种形式的分组。
回答by scartag
You could use the EntityFunctions.DiffDays
method
你可以用这个EntityFunctions.DiffDays
方法
EntityFunctions.DiffDays(product.EventDate, DateTime.Now) //this will return the difference in days
UPDATE
更新
EntityFunctions is now obsolete so you should use DBFunctions instead.
EntityFunctions 现在已过时,因此您应该改用 DBFunctions。
System.Data.Entity.DbFunctions.DiffDays(product.EventDate, DateTime.Now)
回答by Jon Schneider
To add on to scartag's answer,
添加到 Scartag 的答案,
The MSDN documentation for DbFunctions.DiffDaysdoesn't directly mention whether and when the value returned by DiffDays() will be negative, so I thought I'd provide that information here:
DbFunctions.DiffDays的MSDN 文档没有直接提到 DiffDays() 返回的值是否以及何时为负数,所以我想我会在这里提供这些信息:
The result will be negative when the argument 1 date is larger than (i.e. in the future relative to) the argument 2 date.
当参数 1 日期大于(即在未来相对于)参数 2 日期时,结果将为负数。
For example, given a table Deliveries
with a non-null field ScheduledDeliveryDate
that can have values both in the past and in the future relative to the current date, this query will get all records with a delivery date/time within 2 days of the current date/time (both past and future):
例如,给定一个Deliveries
具有非空字段的表,该字段ScheduledDeliveryDate
可以具有相对于当前日期的过去和将来的值,此查询将获取交货日期/时间在当前日期的 2 天内的所有记录/时间(过去和未来):
DateTime now = DateTime.Now;
var results = from d in Deliveries
where (DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) < 2
&& DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) > -2)
select d;
回答by Dmitry Stepanov
This should work:
这应该有效:
using System.Data.Entity.SqlServer;
where (int)SqlFunctions.DateDiff("day", product.EventDate, DateTime.Now) <= 60