我尝试在当前日期的 60 天、30 天、20 天中选择数据库中的记录。
请在下面查看此查询。
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();
但是这个查询发生了错误。
我是无知的。请帮忙。
最佳答案
您可以使用 EntityFunctions.DiffDays
方法
EntityFunctions.DiffDays(product.EventDate, DateTime.Now) //this will return the difference in days
更新
EntityFunctions 现已过时,因此您应该改用 DBFunctions。
System.Data.Entity.DbFunctions.DiffDays(product.EventDate, DateTime.Now)
关于c# - LINQ to Entities 无法识别方法 'System.TimeSpan Subtract(System.DateTime)' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15264319/