Search code examples
nhibernatelinq-to-nhibernatenhibernate-criteria

QueryOver fails with could not resolve property:


I am using NHibernate 3.0 and was comparing Query and QueryOver

var p = _prepo.Query<Party>()
            .Where(c => c.Person.LastName == "Bobby")
            .FirstOrDefault();

The above works, I get proxy class for p.Person if I view the object graph.

var p = _prepo.QueryOver<Party>()
            .Where(c => c.Person.LastName == "Bobby")
            .FirstOrDefault();

This one fails with error ==> could not resolve property: Person.LastName of:

Why?


Solution

  • I'm not familiar with the Linq provider but when using QueryOver you have to use a join to do a query like that:

    Example 1

    IQueryOver<Cat,Kitten> catQuery =
    session.QueryOver<Cat>()
        .JoinQueryOver(c => c.Kittens)
            .Where(k => k.Name == "Tiddles");
    

    Example 2

    Cat catAlias = null;
    Kitten kittenAlias = null;
    
    IQueryOver<Cat,Cat> catQuery =
        session.QueryOver<Cat>(() => catAlias)
            .JoinAlias(() => catAlias.Kittens, () => kittenAlias)
            .Where(() => catAlias.Age > 5)
            .And(() => kittenAlias.Name == "Tiddles");