nhibernatefluent-nhibernatequeryoverdiscriminator

Add restriction on discriminator (class type) to select subset of classes in fluent nhibernate


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.


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