1
我在ActiveRecord的一個問題類具有以下字段:Criteria API - 如何根據收集計數獲取記錄?
[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);
任何想法我做錯了嗎?
感謝毛,我有我的DB三問他們兩個都有相關的分配問題。現在,您的解決方案在大多數情況下會給我三個甚至沒有初始化的Question對象(id#0):( – Cosmo 2010-05-22 11:18:36
這對我來說只適用於SQL:「select * from Question q where(selectQueue中的COUNT(*)rq其中q.Id = rq.ParentId)<3「。任何想法如何移植它? – Cosmo 2010-05-22 12:22:46
@Cosmo:我發佈之前在類似模型上嘗試過我的解決方案,並且它工作正常:http://code.google。 COM/p/mausch /源/瀏覽/中繼/ LazyTests/DivisionTests.cs#100 – 2010-05-22 15:57:15