带有子集合成员条件的NHibernate查询仅返回部分子集合 [英] NHibernate query with criteria on child collection member returns only partial child collection
问题描述
我在Teacher
和StudentReport
之间具有父子关系.当教师完成报告时,每个StudentReport
都有一个时间戳字段记录.我有一个查询要查找所有在一定分钟前已完成一份或多份报告的教师:
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?
更新
这是解决方案:
return Session.QueryOver<Teacher>()
.Fetch(t => t.StudentReports).Eager
.JoinQueryOver<StudentReport>(t => t.StudentReports)
.Where(r => r.FirstSaveTimestamp >= activityCutoff)
.TransformUsing(Transformers.DistinctRootEntity)
.List<Teacher>();
推荐答案
使用访存
return Session.QueryOver<Teacher>()
.Fetch(t => t.StudentReports)
.Where(r => r.FirstSaveTimestamp >= activityCutoff)
.TransformUsing(Transformers.DistinctRootEntity)
.List<Teacher>();
这篇关于带有子集合成员条件的NHibernate查询仅返回部分子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!