Nhibernate 按联接查询并按独立if添加条件

Nhibernate 按联接查询并按独立if添加条件,nhibernate,queryover,Nhibernate,Queryover,我在Nhibernate 3.1中有一个联合查询 Person类通过标识类(一对一)具有关联 Code是Person类的字段,FirstName是Identity类的字段 var q = SessionInstance.QueryOver<Person>() .Where(p => p.Code.IsLike(code,MatchMode.Start)) .Full.JoinQueryOver(p => p.Identity); if (

我在Nhibernate 3.1中有一个联合查询

Person类通过标识类(一对一)具有关联 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>();
var q=SessionInstance.QueryOver()
.Where(p=>p.Code.IsLike(Code,MatchMode.Start))
.Full.JoinQueryOver(p=>p.Identity);
如果(!String.IsNullOrEmpty(firstName))
q=q.Where(i=>i.FirstName.IsLike(FirstName,MatchMode.Anywhere));
返回q.List();
这个结果是正确的,但存在一个问题。搜索不包括Person类中代码字段的按空值排序的项。我更正了以下问题

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>();
var q=SessionInstance.QueryOver()
.Full.JoinQueryOver(p=>p.Identity);
如果(!String.IsNullOrEmpty(代码))
q=q.Where(i=>i.Person.Code.IsLike(Code,MatchMode.Start));
如果(!String.IsNullOrEmpty(firstName))
q=q.Where(i=>i.FirstName.IsLike(FirstName,MatchMode.Anywhere));
返回q.List();
现在,我收到一条运行时错误消息:

无法解析属性:Identity。的代码:MyNameSpace.Domain.Entities.Identity

在两个类之间通过联接进行的查询中,如何通过if添加两个条件(where)

(如果参数!=null)

标识标识符IAS=null;
var q=SessionInstance.QueryOver()
.Full.JoinAlias(p=>p.Identity,()=>identityAlias);
如果(!String.IsNullOrEmpty(代码))
q、 其中(p=>p.Code.IsLike(Code,MatchMode.Start));
如果(!String.IsNullOrEmpty(firstName))
q、 其中(()=>identityAlias.FirstName.IsLike(FirstName,MatchMode.Anywhere));

var q=SessionInstance.QueryOver();
如果(!String.IsNullOrEmpty(代码))
q、 其中(p=>p.Code.IsLike(Code,MatchMode.Start));
如果(!String.IsNullOrEmpty(firstName))
q、 Full.JoinQueryOver(p=>p.Identity)
.Where(i=>i.FirstName.IsLike(FirstName,MatchMode.Anywhere));

当然,第一个解决方案解决了我的问题。第二个解决方案在最后一个if中出现生成错误,原因是此消息:无法将类型“NHibernate.IQueryOver”隐式转换为“NHibernate.IQueryOver”。存在显式转换(是否缺少强制转换?)
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));