是否可以从 LINQ 或 HQL 创建 ICriteria/ICriterion?

发布于 2024-08-30 15:11:49 字数 653 浏览 1 评论 0原文

我正在创建一个方法,可以从我的抽象过滤器对象创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沒落の蓅哖 2024-09-06 15:11:49

嘿,您检查过这个问题吗?它展示了从 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)

寄离 2024-09-06 15:11:49

不,那是不可能的。为什么不使用 linq 而使用 criteria?

No that is not possible. Why don't you use linq instead of criteria?

扛起拖把扫天下 2024-09-06 15:11:49

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文