6

I have a parent-child relationship between Teacher and StudentReport. Each StudentReport has a timestamp field recording when the teacher finished the report. I have a query to find all teachers who have completed one or more of their reports as of 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 child collection for each teacher only contains the reports that match the selection criterion. I would like the report collection of each matching teacher to contain all of the reports, not just the few reports that meet the criteria.

Is there some way I can either load the full child collection eagerly, or omit loading it in this query and rely on lazy loading it?

Update

This is the 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>();
Carl Raymond
  • 4,429
  • 2
  • 25
  • 39
  • Just a random tip unrelated to your question. To negate your intervalMinutes you can write '-intervalMinutes' and it will give you the negative value of the positive number passed in. Instead of having to do '-1 * intervalMinutes' – Phill Jun 01 '11 at 01:20

1 Answers1

2

Use Fetch

return Session.QueryOver<Teacher>()
    .Fetch(t => t.StudentReports)
    .Where(r => r.FirstSaveTimestamp >= activityCutoff)
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Teacher>();
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • That's it! Thanks. I'm still learning the QueryOver syntax, and seeing this example, I think I can clean up some of my other queries. – Carl Raymond May 31 '11 at 21:26