NHibernate query with criteria for a member of a child collection returns only a partial child collection

I have a parent-child relationship between Teacher and StudentReport . Each StudentReport has a time StudentReport record when the teacher completed the report. I have a query to find all teachers who completed one or more of their reports a certain number of minutes ago:

  public IList<Teacher> FindRecentlyActiveTeachers(int intervalMinutes) { if (intervalMinutes <= 0) throw new ArgumentException("Interval must be a positive number of minutes"); DateTime activityCutoff = DateTime.Now.AddMinutes(-1 * intervalMinutes); return Session.QueryOver<Teacher>() .Left.JoinQueryOver<StudentReport>(t => t.StudentReports) .Where(r => r.FirstSaveTimestamp >= activityCutoff) .TransformUsing(Transformers.DistinctRootEntity) .List<Teacher>(); } 

This returns the correct list of teachers, but the children's collection for each teacher contains only those reports that meet the selection criteria. I would like the reports collection of each relevant teacher to contain all reports, and not just a few reports that meet the criteria.

Is there a way by which I can either load the full child collection or not load it into this request, and rely on its lazy loading?

Update

This solution:

  return Session.QueryOver<Teacher>() .Fetch(t => t.StudentReports).Eager .JoinQueryOver<StudentReport>(t => t.StudentReports) .Where(r => r.FirstSaveTimestamp >= activityCutoff) .TransformUsing(Transformers.DistinctRootEntity) .List<Teacher>(); 
+6
source share
1 answer

Use fetch

 return Session.QueryOver<Teacher>() .Fetch(t => t.StudentReports) .Where(r => r.FirstSaveTimestamp >= activityCutoff) .TransformUsing(Transformers.DistinctRootEntity) .List<Teacher>(); 
+2
source

Source: https://habr.com/ru/post/889490/


All Articles