sunil singh
sunil singh

Reputation: 315

Nhibernate Query Over exception on join with invalid value

-- 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();

This Query is throwing Exception when ClientId is not valid. How i can avoid that ?

Upvotes: 1

Views: 413

Answers (1)

Najera
Najera

Reputation: 2879

If you need deal with nullables properties in the queries you can use this:

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();

Upvotes: 1

Related Questions