开发者

Restricting an NHibernate query using ICriteria according to an enumeration of enums

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

W开发者_JAVA技巧hen 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>()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜