user357086
user357086

Reputation: 404

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?

Upvotes: 2

Views: 5782

Answers (2)

Thomas
Thomas

Reputation: 2458

It works when you use Linq in this case because the filtering is being done on the client, not in the database. So it is actually the IEnumerable version of Where that is running which is not related to NHibernate.

The QueryOver uses Expression<Func<T,object>> which NHibernate tries to translate to SQL but fails. For reasons unknown to me you must explicitly join using JoinQueryOver or JoinAlias.

Some more info on QueryOver here: http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

Upvotes: 0

Cole W
Cole W

Reputation: 15313

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

Upvotes: 6

Related Questions