A.R.
A.R.

Reputation: 15704

Nhibernate QueryOver nested properties

Is there an easy way to use QueryOver with nested properties?

For example, I try something like this;

// SPLAT!
session.QueryOver<SuperHero>().Where(Expression.Eq("HomeBase.Name", "Bat Cave");

It won't work because it 'could not resolve property 'homebase.name' of SuperHero. That makes sense, but there is obviously some way to make this work, because if I use the older 'Query' approach I can get it to work just fine, i.e.

// The results I (technically) want.
sess.Query<SuperHero>().Where(x => x.HomeBase.Name == "The Bat Cave");

So what am I missing? I am guessing that there is some way to combine expressions, etc. to get the nexted properties to work with QueryOver, but what are they?

Upvotes: 0

Views: 1914

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

You can't do a nested property access like that--you'll have to join to the related table:

session.QueryOver<SuperHero>()
    .JoinQueryOver(sh => sh.HomeBase)
        .Where(hb => hb.Name == "Bat Cave");

Also you don't need to use strings in your restrictions--that's one of the great advantages of using QueryOver after all.

Upvotes: 3

Related Questions