扩展方法转换为 LINQ 表达式和常用方法
我有一个执行以下操作的扩展方法:
public static bool Between(this DateTime target, DateTime startDate, DateTime endDate)
{
return target >= startDate && target <= endDate;
}
我可以像这样调用它
if (expectedDate.Between(minDate, maxDate)) { do code }
我现在尝试在 Linq/Lambda 表达式中使用它,例如
return list.Where(item => targetDate.Between(item.StartDate, item.EndDate));
OR if (!list.Any(pp => targetDate.Between(pp.StartDate, pp.EndDate)))
,我在运行时收到以下错误:
LINQ to Entities 无法识别“布尔值”方法 Between(System.DateTime, System.DateTime, System.DateTime)' 方法, 并且此方法无法转换为存储表达式。
但这很好,
if (!list.Any(item => targetDate >= item.StartDate && quoteDate.EventDate <=item.EndDate)))
我想要一个通用的方法来调用。我有什么选择?
I have an Extension method that does the following:
public static bool Between(this DateTime target, DateTime startDate, DateTime endDate)
{
return target >= startDate && target <= endDate;
}
and I can call it like this
if (expectedDate.Between(minDate, maxDate)) { do code }
I'm now trying to use this in a Linq/Lambda expression like
return list.Where(item => targetDate.Between(item.StartDate, item.EndDate));
OR if (!list.Any(pp => targetDate.Between(pp.StartDate, pp.EndDate)))
and I get the following error in runtime:
LINQ to Entities does not recognize the method 'Boolean
Between(System.DateTime, System.DateTime, System.DateTime)' method,
and this method cannot be translated into a store expression.
But this is fine
if (!list.Any(item => targetDate >= item.StartDate && quoteDate.EventDate <=item.EndDate)))
I would like to have a common method to call. What are my options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对 Brian 的解决方案进行简单修改,不需要
AsExpandable()
:Simple modification of Brian's solution that doesn't require
AsExpandable()
:由于您没有使用 LINQ to Object,因此查询不会被执行,而是转换为 表达式树将被转换为 SQL。
LINQ to Entities 不知道如何将您编写的方法转换为 SQL,因为您实现了该方法。
我从未使用过 LINQ to Entities,但必须有一种方法来扩展表达式树生成器,以使 LINQ to Entities 能够将您的方法转换为 SQL,LINQ to NHibernate 有办法做到这一点。
这里是一个如何将 LINQ 扩展为实体提供者。
Since you are not using LINQ to Object the query isn't being executed but translated to an expression tree to be translated to SQL.
LINQ to Entities does not know how to translate a method you wrote to SQL since you implemented it.
I never used LINQ to Entities but there must be a way to extend the expression tree builder to enable LINQ to Entities to translate your method to SQL, LINQ to NHibernate has a way to do so.
Here is an example for how to extend the LINQ to Entities provider.
我使用几种不同的方法让它工作。构建 LINQ Between Operator 我创建了一个新方法,
除非我使用 AsExpandable 或 ToList,否则这不起作用()。
AsExpandable() 来自 LinkKit
ToList() 强制它从 Linq 到实体进入 Linq To对象,但执行 SQL。
感谢您的帮助和建议
I got it working using a couple different methods. Building off of LINQ Between Operator I created a new method
This didn't work unless I used AsExpandable or ToList().
AsExpandable() is from LinkKit
ToList() forces it from Linq to entities into Linq To Objects, but executes the SQL.
Thanks for your help and suggestions