14

我需要对子集合进行子查询,但我无法让它工作。

我试过这个

 Task tAlias = null;
        List<Task> result = session.QueryOver<Task>(() => tAlias)
                                   .Where(Restrictions.In(Projections.Property(() => tAlias.Course.Id), courseIds))
                                   .WithSubquery.WhereExists(QueryOver.Of<CompletedTask>().Where(x => x.Student.StudentId == settings.StudentId))                                     
().ToList();

然而我得到

不能在没有投影的条件上使用子查询。

4

1 回答 1

25
session.QueryOver<Task>(() => tAlias)
    .WhereRestrictionsOn(x => x.Course.Id).IsIn(courseIds)
    .WithSubquery.WhereExists(QueryOver.Of<CompletedTask>()
        .Where(x => x.id == tAlias.id) //not sure how you need to link Task to CompletedTask
        .Where(x => x.Student.StudentId == settings.StudentId)
        .Select(x => x.id)) //exists requires some kind of projection (i.e. select clause)
    .List<Task>();

或者,如果您只想要完成的任务,那么只需...

Task taskAlias = null;

session.QueryOver<CompletedTask>()
    .JoinAlias(x => x.Task, () => taskAlias)
    .WhereRestrictionsOn(() => taskAlias.Course.Id).IsIn(courseIds)
    .Where(x => x.Student.StudentId == settings.StudentId)
    .List<CompletedTask>();

或查看在 Task.CompletedTasks 集合上设置学生过滤器。我以前从未使用过此功能。我相信您必须在运行查询之前启用过滤器并设置学生参数。那么您的 Task 对象将仅包含该学生的已完成任务...

http://nhibernate.info/doc/nh/en/index.html#filters

于 2011-05-11T19:45:18.220 回答