Nhibernate QueryOver嵌套属性 [英] Nhibernate QueryOver nested properties
问题描述
是否有使用嵌套属性的QueryOver的简单方法?
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");
它将无法工作,因为它无法解析SuperHero的属性" homebase.name".这是有道理的,但是显然有某种方法可以使它起作用,因为如果我使用较旧的查询"方法,我可以使其工作正常,即
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");
那我想念什么?我猜想有某种方式可以组合表达式等,以使下一个属性可以与QueryOver一起使用,但是它们是什么?
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?
推荐答案
您不能像这样进行嵌套的属性访问-您必须加入相关表:
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");
此外,您无需在限制中使用字符串-毕竟这是使用QueryOver的一大优势.
Also you don't need to use strings in your restrictions--that's one of the great advantages of using QueryOver after all.
这篇关于Nhibernate QueryOver嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!