Search code examples
nhibernatefluent-nhibernate

Nhibernate join tables by multiple columns


I have the following problem with an NHibernate query.

one table

LearnerDocuments :
    LearnerNumber
    CandidateNumber
    ....

other table

LearnerRegistration :
    LearnerId
    LearnerNumber
    ....

the last one

Learner : 
    LearnerId 
    LearnerName 
    ....

The relation between LearnerDocuments and and LearnerRegistration doesn't exist.

How can I write a query that gets all LearnerRegistration which have LearnerDocuments?


Solution

  •  criteria.CreateCriteria(typeof(LearnerRegistration), "av")
                        ......
    
                        .Add(Restrictions.IsNotNull("RegistrationNumber"))                    
                        .Add(Subqueries.PropertyIn("l.Id", detachedCriteria))
    
                     ;
    
    
                var candidate = DetachedCriteria.For<LearnerDocuments>("sd")
                            .SetProjection(Projections.ProjectionList()
                            .Add(Projections.Property("sd.CandidateNumber"))
                            .Add(Projections.Property("sd.RegistrationNumber")))
                            .Add(Restrictions.EqProperty("sd.CandidateNumber", "l.LearnerNumber") 
                            && Restrictions.EqProperty("sd.RegistrationNumber", "lr.RegistrationNumber")
                            );
    
                var proj = Projections.Conditional(Subqueries.Exists(candidate),Projections.Constant(true),Projections.Constant(false));