是否可以从 LINQ 或 HQL 创建 ICriteria/ICriterion?
我正在创建一个方法,可以从我的抽象过滤器对象创建 NHibernate 理解的过滤器(例如,过滤器是指一组 ICriteria 对象)。
public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria)
{
// T4 generated function
// lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) expression trees - hard to generate
// Is there a way to generate HQL/Linq query here istead?
}
然后我想做一些类似
session.CreateCriteria<Entity>().Add(myCriteria.ToNhCriteria())
过滤实体的事情。 问题是使用表达式。方法(Expression.Or 等)非常乏味(该方法是生成的,我有多个 or 语句必须以某种方式连接到一个表达式中)。 有没有办法避免使用 Expression.Or() 并使用 LINQ 或 HQL 创建 ICrietrion / ICriteria?
I am creating a method that can create filter understood by NHibernate (by filter i mean a set of ICriteria object for example) from my abstract filter object.
public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria)
{
// T4 generated function
// lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) expression trees - hard to generate
// Is there a way to generate HQL/Linq query here istead?
}
then i want to do something like
session.CreateCriteria<Entity>().Add(myCriteria.ToNhCriteria())
to filter entities.
The problem is that using Expression. methods (Expression.Or etc) is quite tedious (the method is generated and i have multiple or statements that have to be joined into an expression somehow).
Is there a way to avoid using Expression.Or() and create ICrietrion / ICriteria using LINQ or HQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嘿,您检查过这个问题吗?它展示了从 Linq 到 NHibernate 再到 MultiCriteria(并且在此过程中将 linq 查询转换为 ICriteria)
Hey, did you check out this question? It shows going from Linq to NHibernate to a MultiCriteria (and on the way transforms a linq query to an ICriteria)
不,那是不可能的。为什么不使用 linq 而使用 criteria?
No that is not possible. Why don't you use linq instead of criteria?
Linq 不是最好的解决方案,除非您想使用 WHERE 子句在集合端而不是数据库端进行过滤。
Ayende 建议 ICriteria API 非常适合动态过滤器创建,我在多个 OR 方面遇到的问题已通过使用 Restrictions.Disjunction() 得到解决...这简化了很多
当我问这个问题时,我只是没有意识到 NHibernate 中存在这样的东西:)
Linq is not the best solution unless you want to do filtering on collection-side not on datbase-side using WHERE clauses.
Ayende suggests that ICriteria API is well suited for dynamic filter creation, the problem i had with multiple ORs has been tackled by using Restrictions.Disjunction()... that simplified a lot
At the time I asked the question I just didn't realize such things exist in NHibernate :)