Nhibernate QueryOver nested properties

戏子无情 提交于 2019-12-08 12:24:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!