根据枚举的枚举使用 ICriteria 限制 NHibernate 查询
我有一个实体,带有枚举类型的字段,它作为整数保存在我的数据库中。
当使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最初的猜测是,您需要与枚举成员的整数值进行比较(假设您将枚举映射为整数);所以类似:
可能是您想要的解决方案。如果您用更多详细信息(枚举成员和查询生成的 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:
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.