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

发布于 2024-09-02 10:57:47 字数 1158 浏览 3 评论 0原文

我在 ActiveRecord 中有一个包含以下字段的 Question 类:

[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 查询来获取在latedquestions集合中至少有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);

知道我做错了什么吗?

I have a Question class in ActiveRecord with following fields:

[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; }
    }
}

How do I write a DetachedCriteria query to get all Questions that have at least 5 related questions (count) in the RelatedQuestions collection?

For now this gives me strange results:

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);

Any ideas what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-09-09 10:57:47

尝试类似的方法:

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);

Try something like:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文