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

前端 未结 2 327

I am currently dynamically constructing queries like so:

QueryOver q = QueryOver.Of();

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


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 12:41

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

    Base alias = null;
    
    var query = session.QueryOver(() => alias);
    
    // this statement would be converted to check of the discriminator
    query.Where(o => o is Derived);
    
    var list = query.List();
    

    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(() => alias);
    
    // here we can compare whatever value, we've used as discriminator
    query.Where(Restrictions.Eq("alias.class", "Derived"));
    
    var list = query.List();
    

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

    More details could be found here:

    • 17.1.4.1. Alias and property references

提交回复
热议问题