3

I have a QueryOver by JoinQueryOver In Nhibernate 3.1

The Person class has a association by Identity class (one-to-one) Code is a field of Person class and FirstName is a field of Identity class.

var q = SessionInstance.QueryOver<Person>() .Where(p => p.Code.IsLike(code,MatchMode.Start)) .Full.JoinQueryOver(p => p.Identity); if (!String.IsNullOrEmpty(firstName)) q = q.Where(i => i.FirstName.IsLike(firstName, MatchMode.Anywhere)); return q.List<Person>(); 

that result is correct but, there is a problem. The search does not include items by null value for Code field in Person class. I corrected to following query.

var q = SessionInstance.QueryOver<Person>() .Full.JoinQueryOver(p => p.Identity); if (!String.IsNullOrEmpty(Code)) q = q.Where(i => i.Person.Code.IsLike(code, MatchMode.Start)); if (!String.IsNullOrEmpty(firstName)) q = q.Where(i => i.FirstName.IsLike(firstName, MatchMode.Anywhere)); return q.List<Person>(); 

Now i have a runtime error by this message:

could not resolve property: Identity.Code of: MyNameSpace.Domain.Entities.Identity

in a query by join between two class, How can add two condition(where) by if.

(if parameter != null)

1 Answer 1

3
Identity identityAlias = null; var q = SessionInstance.QueryOver<Person>() .Full.JoinAlias(p => p.Identity, () => identityAlias); if (!String.IsNullOrEmpty(code)) q.Where(p => p.Code.IsLike(code, MatchMode.Start)); if (!String.IsNullOrEmpty(firstName)) q.Where(() => identityAlias.FirstName.IsLike(firstName, MatchMode.Anywhere)); 

or

var q = SessionInstance.QueryOver<Person>(); if (!String.IsNullOrEmpty(code)) q.Where(p => p.Code.IsLike(code, MatchMode.Start)); if (!String.IsNullOrEmpty(firstName)) q.Full.JoinQueryOver(p => p.Identity) .Where(i => i.FirstName.IsLike(firstName, MatchMode.Anywhere)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Of course, the first solution resolved my problem. The second solution has a build error in last if by this message : Cannot implicitly convert type 'NHibernate.IQueryOver<RCISP.Domain.Entities.Person,RCISP.Domain.Entities.Identity>' to 'NHibernate.IQueryOver<RCISP.Domain.Entities.Person,RCISP.Domain.Entities.Person>'. An explicit conversion exists (are you missing a cast?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.