简体   繁体   中英

Nhibernate queryover with multiple join fail

I have a nhibernate queryover like this:

var query = Session.QueryOver<Immobile>()
                                .WhereRestrictionOn(i => i.Agenzia.CodiceAgenzia).IsLike(codiceAgenzia)
                                .WhereRestrictionOn(i => i.StatoImmobile.StatoImmobileId).IsLike(statoId)
                                .And(i => i.Prezzo <= prezzo)
                                .And(i => i.Mq <= metriquadri);

The code compile but on execute I receive this exception:

could not resolve property: Agenzia.CodiceAgenzia of: Domain.Model.Immobile

What am I doing wrong?

QueryOver syntax doesnt work that way unfortunately on Referenced objects you need to join them first and then add the restriction..

Change the code to as follows:

Azengia azengiaAlias=null;   //Azengia here is typeof(Immobile.Azengia) I am assuming it is Azengia
StatoImmobile statoImmobileAlias=null;  //similarly StatoImmobile is assumed to be typeof(Immobile.StatoImmobile)
var query=Session.QueryOver<Immobile>()
.Where(i => i.Prezzo <= prezzo && i.Mq <= metriquadri)
.Inner.JoinAlias(x=>x.Agenzia,()=>azengiaAlias)
.Inner.JoinAlias(x=>x.StatoImmobile,()=.statoImmobileAlias)
.WhereRestrictionOn(() => azengiaAlias.CodiceAgenzia).IsLike(codiceAgenzia)
.WhereRestrictionOn(() => statoImmobileAlias.StatoImmobileId).IsLike(statoId);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM