Nhibernate Criteria Group By 子句并选择其他未分组的数据
我有一个亲子关系,比如说班级和孩子。每个孩子都属于一个班级并有一个年级。我需要选择每班成绩最低的孩子(或孩子的 id)。
session.CreateCriteria(typeof(Classs))
.CreateAlias("Children", "children")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Min("children.Grade"))
.Add(Projections.GroupProperty("Id"))
)
.List<Object[]>();
此查询返回每个班级的最低成绩,但我不知道哪个孩子获得了该成绩。当我将孩子的 ID 添加到组中时,组是错误的,每个孩子都会被返回。
我希望我们能得到这些孩子的 ID,而不需要对他们进行分组。如果这是不可能的,那么也许有一种方法可以通过子查询来解决这个问题?
I have a parent child relationship, let's say class and children. Each child belongs to a class and has a grade. I need to select the children (or the ids of the children) with the lowest grade per class.
session.CreateCriteria(typeof(Classs))
.CreateAlias("Children", "children")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Min("children.Grade"))
.Add(Projections.GroupProperty("Id"))
)
.List<Object[]>();
This query returns me the lowest grade per class, but I don't know which child got the grade. When I add the children's Id to the group, the group is wrong and every child gets returned.
I was hoping we could just get the id's of those childs without grouping them. If this is not possible, then maybe there is a way to solve this with subqueries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果最低成绩是唯一的:
转换为标准
我现在无法测试它,但你应该明白
if min grade is unique:
transalted to Criteria
i cant test it right now, but you should get the idea