Ehsan
Ehsan

Reputation: 3491

Nhibernate QueryOver by Enum Flags

I have a query by QueryOver :

public IList<Person> SearchTest(PersonEnumType type)
{
    var q = SessionInstance.QueryOver<Person>();
    q = q.Where(x => (x.PersonEnumType & type) == type);
    return q.List<Person>();
}

and PersonEnumType is a Enum flags :

[Flags]
public enum PersonEnumType
{
     Employee1 = 1,
     Employee2 = 2,
     Employee3 = 4
}

This throws Could not determine member from (Convert(x.PersonEnumType) & Convert(value(NHibernate.Repository.PersonRepositoryNh+<>c__DisplayClass2).type))

Of course this works in Nhibernate.Linq.

Why?

Upvotes: 2

Views: 1182

Answers (1)

LeftyX
LeftyX

Reputation: 35587

if you've mapped your property properly in your mapping file:

<property name="PersonEnumType" type="MyApp.PersonEnumType, MyApp">
    <column name="Person" default="1" />
</property>

You can achieve what you're looking for using filters.
I don't know if this is the only solution but, here it goes:

You can create a filter definition:

<filter-def name="PersonEnumTypeFilter">
    <filter-param name="personType" type="MyApp.PersonEnumType, MyApp"/>
</filter-def>

and implement it in your class mapping:

<filter name="PersonEnumTypeFilter" condition="(:personType &amp; PersonEnumType) = PersonEnumType"/>

Now you can switch on your filter:

public IList<Person> SearchTest(PersonEnumType type)
{
    SessionInstance.EnableFilter("PersonEnumTypeFilter").SetParameter("personType",   type);
    var q = SessionInstance.Query<Person>();
    return q.ToList<Person>();
}

You can read more about filters here.

Upvotes: 1

Related Questions