Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate QueryOver how to join on non declared relationship

How to do the following join to return Users who have access to a Company given a company id. The problem is there is no explicit relationship using a User object between UserAccess and User they simply join on the string property Username:

User(Username, Name)
UserAccess(Username, Company)
Company(Id)

Session.QueryOver<Company>()
        .Where(c => c.Id == companyId)
        .JoinQueryOver<UserCompanyAccess>(u => u.UserAccessList)
        .JoinQueryOver<User>(u => **Nope no property, just a string**
like image 863
jenson-button-event Avatar asked Aug 16 '11 10:08

jenson-button-event


1 Answers

could be done with a subquery

var subquery = QueryOver.Of<Company>()
    .Where(c => c.Id == companyId)
    .JoinQueryOver<UserCompanyAccess>(u => u.UserAccessList)
    .Select(uca => uca.UserName);

var users = session.QueryOver<User>()
    .WithSubquery.WhereProperty(u => u.Name).In(subquery)
    .List();
like image 54
Firo Avatar answered Jan 02 '23 21:01

Firo