I have a simple test object model in which there are schools schools have a collection of students.
I would like to retrive a school and all its students who are above a certain age.
I carry out the following query in the following manner :
public School GetSchoolAndStudentsWithDOBAbove(int schoolid, DateTime dob)
{
var school = this.Session.CreateCriteria(typeof(School))
.CreateAlias("Students", "students")
.Add(Expression.And(Expression.Eq("SchoolId", schoolid), Expression.Gt("students.DOB", dob)))
.UniqueResult<School>();
return school;
}
Which obtains a given school and the children which are above a certain age.
This all works fine and I can see the query going into SQL, which I run and returns the expected number of rows.
However when I carry out either of the following
foreach (Student st in s.Students)
{
Console.WriteLine(st.FirstName);
}
Assert.AreEqual(s.Students.Count, 3);
It gives me the total number of students in the given school regardless of the preceeding request by running another query.