nhibernatequeryovericriteria

Equals conditions in outer joins with NHibernate ICriteria/QueryOver query


How do I do a equals condition in an outer join in Nhibernate/QueryOver/ICriteria?

The only way I have found to compare surveyRequest.Survey.Id with surveyID below is with IsIn.

SystemUser systemUser= null;
SurveyRequests surveyRequest = null;

var query = Session.QueryOver<SystemUser>(() => systemUser)
    .Left.JoinAlias(
        () => systemUser.SurveyRequests, 
        () => surveyRequest,
        Restrictions.On(()=>surveyRequest.Survey.Id).IsIn(new object []{surveyID }))
//                                                   ^^^^

(I am reusing an earlier query question.)


Solution

  • We can use the .Where() part of Restrictions

    var query = Session.QueryOver<SystemUser>(() => systemUser)
        .Left.JoinAlias(
            () => systemUser.SurveyRequests, 
            () => surveyRequest,
            //Restrictions.On(()=>surveyRequest.Survey.Id).IsIn(new object []{surveyID }))
            Restrictions.Where(()=>surveyRequest.Survey.Id == surveyID ))