Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restriction on collection with disjunction using NHibernate QueryOver

Hi all NHibernate gurus !

Given these two classes :

public class User {
    long Id;
    string Name;
}

public class Project {
    long Id;
    User Owner;
    IList<User> Managers;
    ...
}

I would like to do a query using QueryOver (not using criteria "magic string" aliases), to get all projects having user1 as Owner OR as one of the Managers.

I know how to separately :

  • get projects having user1 as Owner : session.QueryOver<>Project>>().Where(p=>p.Owner == user1)
  • get as a manager : session.QueryOver<>().JoinAlias(p=>p.Managers, ()=>manager).Where(()=>manager == user1)

but I don't know how to write the disjunction.

If someone had an idea, it would help me a lot.

Thanks in advance,

Chris

like image 572
Chris Avatar asked Nov 04 '22 04:11

Chris


1 Answers

Something like:-

User manager = null;

var query = session
    .QueryOver<Project>()
    .JoinAlias(j => j.Managers, () => manager)
    .Where(w => manager.Name == user1 || w.Owner == user1)
    .List<Project>();

edit to change filter from Name to Id (as OP pointed out):-

.Where(w=>manager.Id == user1.Id || w.Owner.Id == user1.Id)

edit2 to change inner to left use

.JoinAlias(j => j.Managers, () => manager).left
like image 124
Rippo Avatar answered Nov 18 '22 09:11

Rippo