title: NHibernate: how to express a specific "group by" query with criteria
body: <p><strong>Question:</strong></p>
  
  <p>What are the criteria/projections that can generate a following query?</p>
  
  <pre><code>SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, 0, 1)
  </code></pre>
  
  <p>(Obviously this one is easier with <code>DISTINCT</code>, but I'll need counts later, when I fix this one).  </p>
  
  <p><hr />
  <strong>My approaches:</strong></p>
  
  <p>My main problem here is with constants, because if I use </p>
  
  <pre><code>Projections.GroupProperty(Projections.SqlFunction(
     "SUBSTRING",
     NHibernateUtil.String,
     Projections.GroupProperty("Name"),
     Projections.Constant(0),
     Projections.Constant(1)
  ))
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, , )
  </code></pre>
  
  <p>which is kind of obvious from NH <a href="http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/trunk/nhibernate/src/NHibernate/Criterion/SqlFunctionProjection.cs?revision=3701&amp;view=markup" rel="nofollow noreferrer">source code</a>, but useless.
  And if I do </p>
  
  <pre><code>Projections.GroupProperty(Projections.SqlFunction(
     "SUBSTRING",
     NHibernateUtil.String,
     Projections.GroupProperty("Name"),
     Projections.GroupProperty(Projections.Constant(0)),
     Projections.GroupProperty(Projections.Constant(1))
  ))
  </code></pre>
  
  <p>then I get</p>
  
  <pre><code>SELECT SUBSTRING(Name, @p0, @p1) FROM Person GROUP BY SUBSTRING(Name, ?, ?)
  </code></pre>
  
  <p>where question marks seem to be some unresolved parameters, but I have no idea why.</p>
  
  <p><hr />
  <strong>More details:</strong>  </p>
  
  <p>I just found</p>
  
  <pre><code>AbstractEntityJoinWalker.InitProjection(
      SqlString projectionString,
      SqlString whereString,
      SqlString orderByString,
      string /* WTF? */ groupByString,
      SqlString havingString,
      LockMode lockMode
  )
  </code></pre>
  
  <p>The type of <code>groupByString</code> looks extremely suspicious.</p>