如何创建子查询Projection,给它一个别名,并使用Criteria API通过NHibernate中的Alias排序

时间:2011-01-07 03:39:03

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

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

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

  

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

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

喝彩!

2 个答案:

答案 0 :(得分:3)

您可以根据需要添加更多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();

希望得到这个帮助。

答案 1 :(得分:1)

以下是我的简单示例:

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