0

Question:

What are the criteria/projections that can generate a following query?

SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, 0, 1)

(Obviously this one is easier with DISTINCT, but I'll need counts later, when I fix this one).


My approaches:

My main problem here is with constants, because if I use

Projections.GroupProperty(Projections.SqlFunction(
   "SUBSTRING",
   NHibernateUtil.String,
   Projections.GroupProperty("Name"),
   Projections.Constant(0),
   Projections.Constant(1)
))

I get

SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, , )

which is kind of obvious from NH source code, but useless. And if I do

Projections.GroupProperty(Projections.SqlFunction(
   "SUBSTRING",
   NHibernateUtil.String,
   Projections.GroupProperty("Name"),
   Projections.GroupProperty(Projections.Constant(0)),
   Projections.GroupProperty(Projections.Constant(1))
))

then I get

SELECT SUBSTRING(Name, @p0, @p1) FROM Person GROUP BY SUBSTRING(Name, ?, ?)

where question marks seem to be some unresolved parameters, but I have no idea why.


More details:

I just found

AbstractEntityJoinWalker.InitProjection(
    SqlString projectionString,
    SqlString whereString,
    SqlString orderByString,
    string /* WTF? */ groupByString,
    SqlString havingString,
    LockMode lockMode
)

The type of groupByString looks extremely suspicious.

4

1 回答 1

0

这应该可以工作(尽管您可能想要 SUBSTRING(Name, 1, 1) 但无论如何):

Projections.SqlGroupProjection(
    "SUBSTRING(Name, 0, 1) as FirstChar",
    "SUBSTRING(Name, 0, 1)",
    new [] {"FirstChar"}, 
    new[] {NHibernateUtil.String}
)
于 2012-05-22T09:07:27.787 回答