문제

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.

도움이 되었습니까?

해결책

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

다른 팁

Animal animal = null;

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

Where Animals is your Discriminator-Enum

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top