根据枚举的枚举使用 ICriteria 限制 NHibernate 查询

发布于 2024-09-24 00:01:20 字数 448 浏览 4 评论 0原文

我有一个实体,带有枚举类型的字段,它作为整数保存在我的数据库中。

当使用 ICriteria 从数据库中检索对象时,我希望将结果限制为该字段是枚举值集合的成员。 Restrictions.In 是否适用于枚举集合?

以下不起作用。我是否必须在查询的“restrictions.in”部分执行类似类型转换的操作?

var myEnumCollection = new MyEnum[] { MyEnum.One };
return FindAll<MyType>(Restrictions.In("EnumProperty", myEnumCollection));

FindAll是一个方法封装

criteria.GetExecutableCriteria(Session).List<MyType>()

I have an entity, with a field of type enum, that is persisted as an integer in my database.

When retrieving objects from the database using ICriteria, I wish to restrict the results to those with the field being a member of a collection of enum values. Does Restrictions.In work with a collection of enums?

The following does not work. Do I have to perform something like type-casting at the "restrictions.in" part of the query?

var myEnumCollection = new MyEnum[] { MyEnum.One };
return FindAll<MyType>(Restrictions.In("EnumProperty", myEnumCollection));

FindAll is a method encapsulating

criteria.GetExecutableCriteria(Session).List<MyType>()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无所的.畏惧 2024-10-01 00:01:20

我最初的猜测是,您需要与枚举成员的整数值进行比较(假设您将枚举映射为整数);所以类似:

var myEnumCollection = new int[] { MyEnum.One }; 
return FindAll<MyType>(Restrictions.In("EnumProperty", myEnumCollection));

可能是您想要的解决方案。如果您用更多详细信息(枚举成员和查询生成的 sql 的映射)更新您的帖子,我也许能够提供进一步的帮助。

My initial guess would be that you'll need to compare against the integer values of the enum members (assuming that you're mapping the enum as an integer); so something like:

var myEnumCollection = new int[] { MyEnum.One }; 
return FindAll<MyType>(Restrictions.In("EnumProperty", myEnumCollection));

May be the solution that you're after. If you update your post with further details (mapping of the enum member and the sql being generated by the query), I may be able to provide further assistance.

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