问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/15296162/nhibernate-queryover-nested-properties