【问题标题】:How to create a subquery Projection, give it an Alias, and sort by the Alias in NHibernate with the Criteria API如何创建子查询投影,给它一个别名,并在 NHibernate 中使用 Criteria API 按别名排序
【发布时间】:2011-01-07 03:39:02
【问题描述】:

forum.hibernate.org/viewtopic.php?p=2378849

其中一位海报给出了这个答案:

您需要创建一个 Projection (...),给它一个别名,然后您可以按别名排序。 没有时间发布详细信息,但我很确定这会奏效。

有人可以提供一个简单的示例,使用 Criteria API,查询使用 Projection 执行 子查询,然后将该子查询用作别名,然后按该别名排序?

干杯!

【问题讨论】:

    标签: c# nhibernate criteria-api nhibernate-projections


    【解决方案1】:

    您可以根据需要添加更多 DetachedCriteria。

    这是我的示例:

    DetachedCriteria sum = DetachedCriteria.For(typeof(MasterAsset), "asset2")
                        .SetProjection(Projections.Sum("PhysicCondition"));
    
    DetachedCriteria count = DetachedCriteria.For(typeof(MasterAsset), "asset3")
                        .SetProjection(Projections.Count("PhysicCondition"));
    
    Session.CreateCriteria(typeof(MasterAsset), "asset1")
                        .SetProjection(Projections.ProjectionList()
                        .Add(Projections.Property("IDMasterAsset"), "IDAsset"))
                        .Add(Subqueries.PropertyLt("PhysicCondition", sum))
                        .Add(Subqueries.PropertyLe("PhysicCondition", count))
                        .AddOrder(Order.Asc("IDAsset"))
                        .List();
    

    希望对您有所帮助。

    【讨论】:

    【解决方案2】:

    这是我的简单示例:

    DetachedCriteria sum = DetachedCriteria.For(typeof(MasterAsset), "asset2")
                      .SetProjection(Projections.Sum("PhysicCondition"));
    Session.CreateCriteria(typeof(MasterAsset), "asset1")
              .SetProjection(Projections.ProjectionList().Add(Projections.Property("IDMasterAsset"), "IDAsset"))
              .Add(Subqueries.PropertyLt("PhysicCondition", sum))
              .AddOrder(Order.Asc("IDAsset"))
              .List();     
    

    【讨论】:

    • 嘿,谢谢 bantencity,虽然我想要一个使用子查询的示例,而不仅仅是 sum。有任何想法吗?我已经编辑了我的问题以使其更清楚。再次感谢
    • 感谢bantencity,其实你的回答很好,我的问题很糟糕!我会尝试问一个不同的问题
    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多