我在 Nhibernate 3.1 中有一个 JoinQueryOver 的 QueryOver
Person类有一个Identity类的关联(一对一) Code 是 Person 类的字段,FirstName 是 Identity 类的字段。
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>();
结果是正确的,但是有一个问题。搜索不包括 Person 类中 Code 字段的空值项目。我更正了以下查询。
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>();
现在我收到此消息的运行时错误:
could not resolve property: Identity.Code of: MyNameSpace.Domain.Entities.Identity
在两个类之间的连接查询中,如何通过 if 添加两个条件(where)。
(如果参数 != 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));
或
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));
关于nhibernate - QueryOver by join and add conditions by Independent if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7452599/