Критерии API - Как получить записи на основе количества коллекций? - PullRequest
1 голос
/ 22 мая 2010

У меня есть класс Question в 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, чтобы получить все Вопросы, у которых есть как минимум 5 связанных вопросов (кол-во) в коллекции RelatedQuestions?

Пока это дает мне странные результаты:

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

Есть идеи, что я делаю не так?

1 Ответ

2 голосов
/ 22 мая 2010

Попробуйте что-то вроде:

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