2

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?

Ehsan
  • 3,431
  • 8
  • 50
  • 70
  • My guess is that with Linq the operation is done in memory so it will first load all the persons and then do the x.PersonEnumType & type filtering in memory. Or maybe the linq provider is just that good. Anyway [you can do it using ICriteria](http://stackoverflow.com/questions/1835392/hql-to-criteriaquery-when-using-bitwise-operators) – Toni Parviainen Feb 21 '12 at 16:42

1 Answers1

1

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.

LeftyX
  • 35,328
  • 21
  • 132
  • 193