开发者

nhibernate querying a collection

Imagine a database structure where you have a Class and Student, and Student has one class, and vice-versa, a class has many students.

How would one go in Nhibernate to create a query, (Preferably QueryOver or Criteria API), for the following.

Select ANY class which contains BOTH StudentA and StudentB.
开发者_Go百科

If it was a disjunction (OR), I could do it by specifying

List<Student> studentRestrictionList = new List<Student>();
studentRestrictionList.Add(studentA);
studentRestrictionList.Add(studentB);
Student studentAlias = null;
var q = session.QueryOver<Class>();
q.Left.JoinAlias(item => item.Students, () => studentsAlias);
q.WhereRestrictionOn(() => studentsAlias.ID)IsInG(studentRestrictionList);

The problem starts when I need to do a conjunction (AND).

Any ideas?


To get conjunction, I would create a separate conjunction object and attach it to the criteria(again, untested but should get you on the right track):

var q = DetachedCriteria.For<Class>()
 .CreateCriteria("this.Students", "student");

var con = new Conjunction();
foreach(var s in studentList)
{
 con.Add(Restrictions.Eq("student.Id", s.Id));
}

q.Add(con);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜