- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在阅读一些帖子,但我没有找到 LINQ To Entities、Lambda 表达式和 DateTime.AddMonth
问题的解决方案。
问题是我正在尝试在 Lambda 表达式中使用 DateTime.AddMonth
,但出现此错误:
"LINQ to Entities does not recognize the method 'System.DateTime AddMonths(Int32)' method, and this method cannot be translated into a store expression"
当我执行这段代码时:
List<Orders> orders = context.Orders
.Where(o => o.IdOrderStatus == 1)
.Where(o => o.PaymentDate.Value.AddMonths(o.Products.ProductCategories.CommissionableMonths) > DateTime.Now)
.ToList();
有没有办法避免这种异常但保持相同的行为?
我不太了解 Linq、Lambdas 或 Entity Framework 。
非常感谢您!
Gsus.
最佳答案
您可以在不过滤日期的情况下返回必要的信息,然后再过滤日期。 (当然,我不确定您的数据有多大,因此这可能效率不高。如果是这样,您将需要某种基于 SQL 的解决方案——也许是一个存储过程。)
List<Orders> orders = context.Orders
.Where(o => o.IdOrderStatus == 1)
.ToList();
orders = orders.Where(o.PaymentDate.Value.AddMonths(o.Products.ProductCategories.CommissionableMonths) > DateTime.Now);
这会将查询的 AddMonths 部分转换为 Linq-to-Objects 方法而不是 Linq-to-Entities 调用。
关于c# - LINQ to Entities : Using DateTime. Lambda 表达式中的 AddMonth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1064620/
我有一个单元测试,其中预期结果是 DateTime 并且设置如下: var expectedResult = DateTime.Today.AddMonths(3).AddMonths(3); 之后,
我有一个跟踪账单的程序。如果帐单每月重复一次,我想将这些日期(最多一年)添加到列表中。 //code changed for brevity but same principle applies pr
如果我有 DateTime date = new DateTime("1/31/2010"); 并且我调用 date.AddMonth(1)。我会收到“2/28/2010”还是会因为“2/31/201
比方说,我有 2010 年 2 月 28 日,并使用 AddMonths(1)... 在该日期上加一个月结果日期是 3 月 28 日,但不是我想要的 3 月 31 日。 有没有办法稍微调整一下,这样就
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这是一个简单的例子(今天是 2016-08-29): var_dump(Carbon::now()); var_dump(Carbon::now()->addMonths(6)); 输出: objec
这是我的代码: return Newsletterctx.Subscribers.Count(o => o.Validated == false && o.ValidationE
Controller : DateTime startDate = DateTime.Now; ViewData["now"] = startDate.ToString
如果我使用太大的 int 在 DateTime 对象上调用 AddMonths,我会收到一个 ArgumentException 抛出在我的脸上,并带有一条礼貌的消息,说, “添加或减去的值导致无法表
我正在使用 Netbeans 8。我仍在学习计算机科学的初级类(class)。我正在尝试编写两种方法,即 addDays 和 addMonths。下面是方法 public void addMonths
我一直在阅读一些帖子,但我没有找到 LINQ To Entities、Lambda 表达式和 DateTime.AddMonth 问题的解决方案。 问题是我正在尝试在 Lambda 表达式中使用 Da
我有客户端日期验证,要求一个特定日期距另一日期为一个月,因此我使用d.setMonth(d.getMonth() + 1)并且大多数情况下工作得很好。 对于 2009 年 1 月 31 日的月末问题,
我正在使用PowerShell来获取之前的6个月。今天的日期4-15-2020使用foreach循环 $months = ((get-date).AddMonths(-1).month,(get-da
我是一名优秀的程序员,十分优秀!