views:

46

answers:

1

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

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.

DanP