How can QueryOver be used to filter for a specific class?

非 Y 不嫁゛ 提交于 2019-12-01 08:05:24

This is not intuitive, but the following should work fine (QueryOver):

if (bar) q = q.Where(b => b.GetType() == typeof(Derived));

I'm not sure about a way to do this in LINQ-to-NH.

The general QueryOver, asking for a subtype, would look like this:

Base alias = null;

var query = session.QueryOver<Base>(() => alias);

// this statement would be converted to check of the discriminator
query.Where(o => o is Derived);

var list = query.List<Derived>();

But this would result in a statement, expecting that discirminator is "MyNamespace.Derived". If this si not the case, we can use this approach:

Base alias = null;

var query = session.QueryOver<Base>(() => alias);

// here we can compare whatever value, we've used as discriminator
query.Where(Restrictions.Eq("alias.class", "Derived"));

var list = query.List<Derived>();

Here we are using the feature of NHibernate: ".class" which does return the value of discriminator

More details could be found here:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!