Group By Property without adding it to the select clause

Description

It would be good to be able to specify a group by without having the property added the select clause.

For example:

This will not work as the GroupProperty will add the property to the select list and doing a property equality on a subquery requires a single column being projected from the subquery.

For this to be implemented I think a new property on the IProjection interface is required, for example a GroupOnly property, the ToSqlString function on the ProjectionsList class can then look at this property and skip group only projections. A new overload for the Projections.GroupProperty would also be needed, for example GroupProperty(string propertyName, bool groupOnly).

If someone thinks this is a good idea I can write a patch for it.

Environment

None

Activity

Peter van der Woude September 26, 2017 at 5:47 AM
Edited

Hi, this QueryOver query (see below) has the exact same problem described - the GROUPBY field is added to the SELECT clause. Which means it fails to run, throwing an exception - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

So just wanting to point out it's not just a problem for the Criteria API (as this issue is labelled) but also the QueryOver API

Unless there is a workaround, I would be very glad to have it

martinp July 12, 2013 at 5:29 PM

Hi all. Just to be sure, there is a workaround for this using QueryOver or I must rewrite my query with native sql?

Andrea Montemaggio September 17, 2011 at 3:56 PM

Here is another use case where I need this feature. I'm trying to add dynamic ordering for queries built by criteria API, and when I detect a "GROUP BY" clause in the root criteria I need to add all ordering fields to the "ORDER BY" clause and the "GROUP BY" clause (really "paths" because may be from joined classes also). The trouble is that adding to the "GROUP BY" clause produces a new projection, so a call to "List<T>" throws an exception: "System.ArgumentException: The value "System.Object[]" is not of type "T" and cannot be used in this generic collection.".

Carina Méndez Rodríguez February 5, 2010 at 4:59 AM

Hi, I'm new here. But I'm using NHibernate since quite a long time and now got the moment when I need exactly this funcionality.
My code for the moment is the following:

DetachedCriteria query = DetachedCriteria.For(typeof("type1"));
DetachedCriteria subquery = DetachedCriteria.For(typeof("type2"));

subquery.SetProjection(Projections.ProjectionList()
.Add(Projections.Max("Id"))
.Add(Projections.GroupProperty("grouping_field"))
);
query.Add(Subqueries.PropertyIn("Id", subquery));

But the problem is the one you guys just mentioned previously.
The subquery returns both Id en the grouping_field.

Dou you already have a solution for this problem? Would be quite nice Otherwise I will have to go back to HQL :|

Thanks in advance.

AlexP January 7, 2010 at 2:55 AM

If there's someone else, i wouldn't mind.
If not - i'm almost done with my primary project, so i will continue in about next week.

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created August 4, 2008 at 7:57 AM
Updated September 26, 2017 at 8:11 AM
Who's Looking?