QueryOver by join and add conditions by Independent if
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)
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));
精彩评论