5

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

Chris
  • 61
  • 5

1 Answers1

2

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
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Thanks, In fact we must do the equality on the objects' IDs and not on objects .Where(w=>manager.Id == user1.Id || w.Owner.Id == user1.Id) Howover, this does not give the expected result because it does an inner join between Project and User, and hence, does not get projects having user1 as owner but not having yet any manager :( – Chris Mar 10 '12 at 12:33
  • Sorry I wasn't 100% sure what you wanted to filter on and you did not mention you wanted a left join... – Rippo Mar 10 '12 at 12:39
  • Don't be sorry, you couldn't know :) .Left.JoinAlias(...) did the trick. Thanks a lot Rippo ! – Chris Mar 10 '12 at 13:33