Search code examples
linqnhibernatenhibernate-criteriaqueryover

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


I am currently dynamically constructing queries like so:

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

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

Now there are multiple mapped subclasses of Base (e. g. Derived) which I want to filter on, 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

How do I best achieve that, preferably - but not neccessarily - in a type-safe way? Can this be done using LINQ, too?


Solution

  • 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.