Question

I'm using fluent nhibernate with Discriminator for subclasses. (very similar to this question)

For example, assume I have classes Cat, Dog, and Racoon that extend abstract class Animal.

I want to be able to select both Cat, And Dog but leave out Racoon. So

return _db.CreateCriteria<Cat>.List<Cat>();

will not work for me as getting list of cats and dogs and merging them seems like a wrong way of doing it.

I've tried doing

this.AndRestrictionOn(Restrictions.In(...))

and it's variants but it always results in errors.

Is there a way I can specify what subclasses I want in the Query Object, please?


Digging through some more, I found out you can do that in HQL

 from Eg.Cat cat where cat.class = Eg.DomesticCat

But I am still unable to convert that into ICriteria / Query object.

Was it helpful?

Solution

Untested, but something like this should work

this.Where(Restrictions.Disjunction()
    .Add(Restrictions.Eq("class", typeof(Cat)))
    .Add(Restrictions.Eq("class", typeof(Dog))));

See http://www.nhibernate.info/doc/nh/en/index.html#queryhql-where

OTHER TIPS

Animal animal = null;

QueryOver<Animal>(() => animal)
 .Where(() => animal.GetType().IsIn(new[] { Animals.Cat.ToString(), Animals.Dog.ToString() }))

Where Animals is your Discriminator-Enum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top