Criteria API - 如何根据集合计数获取记录?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试类似的方法:
Try something like: