c# - LINQ to Entities 无法识别方法 'System.TimeSpan Subtract(System.DateTime)' 方法

标签 c# linq entity-framework linq-to-sql linq-to-entities

我尝试在当前日期的 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();

但是这个查询发生了错误。

enter image description here

我是无知的。请帮忙。

最佳答案

您可以使用 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/

相关文章:

c# - 静态类最佳实践

c# - 搜索文本格式,先上,其余下

entity-framework - 使用 EF Code First 删除整个实体图,包括关系

c# - Entity Framework 语法与 DBset 混淆

c# - LinqKit 谓词 Or 与 Contains 计算结果为等于

c# - 统一: Will get a StackOverflowException if I try to get a singleton that is registered in multiple mappings

c# - XAML:如何更改特定 ResourceDictionary 的来源

c# - 如果 DiagnosticAnalyzer 具有空的 Initialize,则 GetAnalyzerDiagnosticsAsync 不返回任何内容

c# - 在对象列表中搜索任何和所有匹配项

c# - Linq to SQL 并在 ASP.NET MVC 5 Identity 中选择用户的项目