NHibernate ICriteria - 按子集合计数排序?

发布于 2024-09-11 16:55:56 字数 149 浏览 6 评论 0原文

这是一个简单的示例场景 -

一个“标签”有许多“问题”。当我获取标签列表时,如何使用 Criteria API 按标签问题计数进行排序?

我以前做过这个,但大约 4 个月没有碰过 NH 并且忘记了......也许是预测?帮助!!!

谢谢

Here is a simple example scenario -

A 'Tag' has many 'Questions'. When I get a list of Tags how do I order by the Tags Question Count using the Criteria API?

I have done this before but haven't touched NH in about 4 months and have forgotten... projections maybe? help!!!

Thanks

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

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

发布评论

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

评论(2

夜深人未静 2024-09-18 16:55:56

坐下来用一双新的眼睛找出答案...标签现在按标签问题集合(视图)上的属性排序..这在我的领域比按儿童数量排序更有意义

    public IList<Tag> GetTop(int numberOfTags)
    {
        using (ITransaction transaction = Session.BeginTransaction())
        {

            DetachedCriteria detachedCriteria = DetachedCriteria.For<Tag>()
                    .CreateCriteria<Tag>(x => x.Questions)
                    .AddOrder<Question>(x => x.Views, Order.Desc)
                    .SetMaxResults(numberOfTags)
                    .SetProjection(Projections.Distinct(Projections.Id()));

            IList<Tag> tags = Session.CreateCriteria<Tag>()
                .SetFetchMode<Tag>(x => x.Questions,FetchMode.Join)
                .Add(LambdaSubquery.Property<Tag>(x => x.Id).In(detachedCriteria))
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .List<Tag>();

            transaction.Commit();
            return tags;
        }
    }

Sat down with a fresh pair of eyes and figured it out... Tags are now ordered by a propery on the Tags Question collection (views).. which made alot more sence in my domain than ordering by the count of children

    public IList<Tag> GetTop(int numberOfTags)
    {
        using (ITransaction transaction = Session.BeginTransaction())
        {

            DetachedCriteria detachedCriteria = DetachedCriteria.For<Tag>()
                    .CreateCriteria<Tag>(x => x.Questions)
                    .AddOrder<Question>(x => x.Views, Order.Desc)
                    .SetMaxResults(numberOfTags)
                    .SetProjection(Projections.Distinct(Projections.Id()));

            IList<Tag> tags = Session.CreateCriteria<Tag>()
                .SetFetchMode<Tag>(x => x.Questions,FetchMode.Join)
                .Add(LambdaSubquery.Property<Tag>(x => x.Id).In(detachedCriteria))
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .List<Tag>();

            transaction.Commit();
            return tags;
        }
    }
爱的那么颓废 2024-09-18 16:55:56

尝试:

var tags = Session.CreateCriteria(typeof(Tag))
                  .AddOrder(Order.Asc("Tag.Question.Id")
                  .List<Tag>();

// If that does not work, try:
var tags = Session.CreateCriteria(typeof(Tag))
                  .CreateCriteria("Question", "TagQuestion", JoinType.InnerJoin)
                  .AddOrder(Order.Asc("TagQuestion.Id")
                  .List<Tag>();
  1. 对连接列进行排序。< /a>
  2. 对查询条件进行排序

编辑:除非您决定反对它或者您已经熟悉 Criteria API,否则您应该查看 HQLNHibernate.Linq

var tags = Session.Linq<Tag>()
               .OrderBy(tag => tag.Question.Id)
               .ToList();

Linq to NHibernate:巨大的改进。

Try:

var tags = Session.CreateCriteria(typeof(Tag))
                  .AddOrder(Order.Asc("Tag.Question.Id")
                  .List<Tag>();

// If that does not work, try:
var tags = Session.CreateCriteria(typeof(Tag))
                  .CreateCriteria("Question", "TagQuestion", JoinType.InnerJoin)
                  .AddOrder(Order.Asc("TagQuestion.Id")
                  .List<Tag>();
  1. Ordering on joined columns.
  2. Ordering the query criteria.

Edit: Unless you're deciding against it or your already comfortable with the Criteria API, you should take a look at either HQL or NHibernate.Linq:

var tags = Session.Linq<Tag>()
               .OrderBy(tag => tag.Question.Id)
               .ToList();

Linq to NHibernate: a vast improvement.

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