Nhibernate Criteria API-如何根据收集计数获取记录?

Nhibernate Criteria API-如何根据收集计数获取记录?,nhibernate,hibernate,activerecord,criteria,castle-activerecord,Nhibernate,Hibernate,Activerecord,Criteria,Castle Activerecord,我在ActiveRecord中有一个带有以下字段的问题类: [ActiveRecord("`Question`")] public class Question : ObcykaniDb<Question> { private long id; private IList<Question> relatedQuestions; [PrimaryKey("`Id`")] private long Id { get { re

我在ActiveRecord中有一个带有以下字段的问题类:

[ActiveRecord("`Question`")]
public class Question : ObcykaniDb<Question> {

    private long id;
    private IList<Question> relatedQuestions;

    [PrimaryKey("`Id`")]
    private long Id {
        get { return this.id; }
        set { this.id = value; }
    }

    [HasAndBelongsToMany(typeof(Question), ColumnRef = "ChildId", ColumnKey = "ParentId", Table = "RelatedQuestion")] 
    private IList<Question> RelatedQuestions {
        get { return this.relatedQuestions; }
        set { this.relatedQuestions = value; }
    }
}
如何编写DetachedCriteria查询以使RelatedQuestions集合中至少有5个相关问题的所有问题都计数

现在这给了我奇怪的结果:

DetachedCriteria dCriteria = DetachedCriteria.For<Question>()
            .CreateCriteria("RelatedQuestions")
            .SetProjection(Projections.Count("Id"))
            .Add(Restrictions.EqProperty(Projections.Id(), "alias.Id"));

DetachedCriteria dc = DetachedCriteria.For<Question>("alias").Add(Subqueries.Le(5, dCriteria));
IList<Question> results = Question.FindAll(dc);
你知道我做错了什么吗?

试试以下方法:

var dc = DetachedCriteria.For<Question>()
    .SetProjection(Projections.ProjectionList()
                       .Add(Projections.GroupProperty("Id")))
    .Add(Restrictions.Ge(Projections.Count("RelatedQuestions"), 5))
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Question)));
var questions = Question.FindAll(dc);

谢谢毛里西奥,我的数据库里有三个问题,其中两个有相关的问题。现在,您的解决方案在大多数情况下为我提供了三个甚至未初始化的问题对象id0:这对我来说仅使用SQL:select*from Question q其中select COUNT*from RelatedQuestion rq其中q.Id=rq.ParentId<3。知道如何移植吗?@Cosmo:在发布之前,我在一个类似的模型上尝试了我的解决方案,效果很好: