NHibernate的QueryOver组由不选择按列分组 [英] NHibernate QueryOver group by without selecting the grouped by column
问题描述
有类似下面的查询:
var subquery = SessionFactory.GetCurrentSession()
.QueryOver<SomeEntity>()
.Where(_ => _.SomeOtherEntity.Id == someId)
.SelectList(list => list
.SelectGroup(x => x.SomeGroupByProperty)
.SelectMax(x => x.MaxPerGroupProperty))
.List<dynamic>();
生成的SQL是选择这两个 SomeGroupByProperty
和最大 MaxPerGroupProperty
的。是否有可能得到它的小组 SomeGroupByProperty
,但只能选择最高 MaxPerGroupProperty
的?这是使用子查询结果与父查询中包含
The generated sql is selecting both SomeGroupByProperty
and maximum of MaxPerGroupProperty
. Is it possible to get it to group on SomeGroupByProperty
but only select maximum of MaxPerGroupProperty
? This is for using the subquery result with a contains in parent query.
推荐答案
这是在NHibernate的JIRA一个开放的问题(标准查询):的 https://nhibernate.jira.com/browse/NH-1426
It's an open issue in NHibernate jira (criteria query): https://nhibernate.jira.com/browse/NH-1426
您可以像下面这样做虽然
You can do it like this though
var subquery =
QueryOver.Of<SomeEntity>()
.Where(_ => _.SomeOtherEntity.Id == someId)
.Select(
Projections.ProjectionList()
.Add(Projections.SqlGroupProjection("max(MaxPerGroupProperty) as maxAlias", "SomeGroupByProperty",
new string[] { "maxAlias" }, new IType[] { NHibernate.NHibernateUtil.Int32 })));
var parentQuery = session.QueryOver<SomeEntity2>()
.WithSubquery.WhereProperty(x => x.MaxPerGroupPropertyReference).In(subquery).List();
不太为pretty与使用实体属性,但它确实工作。
Not quite as pretty as using the entity properties, but it does work.
这篇关于NHibernate的QueryOver组由不选择按列分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!