ADIMO
ADIMO

Reputation: 1117

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?

Upvotes: 0

Views: 974

Answers (1)

Baz1nga
Baz1nga

Reputation: 15579

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.

Upvotes: 1

Related Questions