![](/img/trans.png)
[英]NHibernate Criteria API error with create alias and a collection of projections
[英]How to create a subquery Projection, give it an Alias, and sort by the Alias in NHibernate with the Criteria API
forum.hibernate.org/viewtopic.php?p=2378849
其中一张海报给出了这个答案:
您需要创建一个Projection(...),给它一个别名,然后您可以按别名排序。 没有时间发布细节,但我很确定这会有效。
有人可以使用Criteria API提供一个使用Projection进行子查询的查询的简单示例,然后将该子查询用作别名,然后通过该Alias进行排序吗?
干杯!
您可以根据需要添加更多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();
希望这有帮助。
这是我的简单示例:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.