1

我正在将流利的 nhibernate 与 Discriminator 一起用于子类。(非常类似于这个问题

例如,假设我有扩展抽象类 Animal 的类 Cat、Dog 和 Racoon。

我希望能够同时选择 Cat 和 Dog 但忽略 Racoon。所以

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

对我不起作用,因为获取猫和狗的列表并合并它们似乎是一种错误的做法。

我试过做

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

它是变体,但总是会导致错误。

请问有没有办法可以在查询对象中指定我想要的子类?


进一步挖掘,我发现你可以在 HQL中做到这一点

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

但我仍然无法将其转换为 ICriteria / Query 对象。

4

2 回答 2

1

未经测试,但这样的东西应该可以工作

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

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

于 2014-03-31T15:56:04.240 回答
0
Animal animal = null;

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

Animals 是你的鉴别器枚举

于 2018-04-24T15:12:15.550 回答