Nhibernate查询超过无效值的连接异常

时间:2015-07-16 12:34:09

标签: c# nhibernate queryover

-- tbl_Region--
ID  DisplayName Description IsUsingLineNumbers    WaitPeriod     FK_StateId
1   UPS            Upstate         0                   10          1
2   DWN            Downstate       1                   20          1
3   OK             Oklahoma        1                   15          2

-- ClientIdentifier-- 
ID  ClientId    FK_RegionId
1   PRCI1             1
2   PRCI2             2
3   PRCI3             3

var clientIdentifier =  session.QueryOver<ClientIdentifier>()
    .Where(x => x.ClientId == clientId).JoinQueryOver(x=>x.Region)
    .SingleOrDefault();
  

当ClientId无效时,此查询抛出异常。我怎么   可以避免吗?

1 个答案:

答案 0 :(得分:1)

如果您需要在查询中处理nullables属性,可以使用:

var query = session.QueryOver<ClientIdentifier>()

if (clientId == null)
    query = query.WhereRestrictionOn(x => x.ClientId).IsNull;
else
    query = query.Where(x => x.ClientId == clientId);

var clientIdentifier =  query.JoinQueryOver(x => x.Region)
    .SingleOrDefault();