How can I use QueryOver to filter for a specific class?

I am currently dynamically creating the following queries:

QueryOver<Base, Base> q = QueryOver.Of<Base>();

if (foo != null) q = q.Where(b => b.Foo == foo);
// ...

Now there are a few mapped subclasses Base(e.g. Derived) that I want to filter, basically something like:

if (bar) q = q.Where(b => b is Derived); // does not work

or

if (bar) q = q.Where(b => b.DiscriminatorColumn == 'derived'); // dito

What is the best way for me to achieve this, preferably - but not necessarily - with a safe type? Can this be done with LINQ?

+4
source share
2 answers

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

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

I am not sure how to do this in LINQ-to-NH.

+8
source

QueryOver, subtype, :

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>();

, , discirminator "MyNamespace.Derived". , :

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>();

NHibernate: ".class",

:

+1

Source: https://habr.com/ru/post/1526163/


All Articles