LINQ to Entities does not recognize the method #39;System.TimeSpan Subtract(System.DateTime)#39; method(LINQ to Entities 无法识别“System.TimeSpan Subtract(System.DateTime)方法)
问题描述
我尝试在当前日期的 60 天、30 天和 20 天内选择数据库中的记录.
I try to select records in database in 60 days 30 days 20 days differents in current date.
请在下面查看此查询.
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();
但是这个查询出错了.
我一无所知.请帮忙.
推荐答案
最简单的方法是在执行查询之前计算边界:
The simplest approach is to work out the bounds before you 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
...
请注意,您当前的查询甚至没有任何意义,因为每个或"d 子句都表明给定的计算小于或等于一个值并且大于或等于相同的值.如果您想要简单的等于",请使用它.如果不是,则不清楚您要尝试做什么.
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 and greater than or equal to the same value. If you want simple "equal to" then use that. If not, it's not clear what you are trying to do.
如果您尝试将值分为小于 20"、20-30"、30-60"、大于 60",则需要使用某种形式的分组.
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.
这篇关于LINQ to Entities 无法识别“System.TimeSpan Subtract(System.DateTime)"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LINQ to Entities 无法识别“System.TimeSpan Subtract(Syst


基础教程推荐
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 创建属性设置器委托 2022-01-01