NHibernate:如何表达特定的“group by”带条件查询
问题:
可以生成以下查询的标准/预测是什么?
SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, 0, 1)
(显然,使用 DISTINCT
会更容易,但是稍后当我修复这个问题时,我需要进行计数)。
My approaches:
我这里的主要问题是常量,因为如果我使用,
Projections.GroupProperty(Projections.SqlFunction(
"SUBSTRING",
NHibernateUtil.String,
Projections.GroupProperty("Name"),
Projections.Constant(0),
Projections.Constant(1)
))
我会得到
SELECT SUBSTRING(Name, 0, 1) FROM Person GROUP BY SUBSTRING(Name, , )
NH 源代码,但没用。 如果我这样做,
Projections.GroupProperty(Projections.SqlFunction(
"SUBSTRING",
NHibernateUtil.String,
Projections.GroupProperty("Name"),
Projections.GroupProperty(Projections.Constant(0)),
Projections.GroupProperty(Projections.Constant(1))
))
那么我会得到
SELECT SUBSTRING(Name, @p0, @p1) FROM Person GROUP BY SUBSTRING(Name, ?, ?)
问号似乎是一些未解决的参数,但我不知道为什么。
More details:
我刚刚发现
AbstractEntityJoinWalker.InitProjection(
SqlString projectionString,
SqlString whereString,
SqlString orderByString,
string /* WTF? */ groupByString,
SqlString havingString,
LockMode lockMode
)
groupByString
的类型看起来非常可疑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效(尽管您可能需要 SUBSTRING(Name, 1, 1) 但无论如何):
This should work (although you might want SUBSTRING(Name, 1, 1) but anyway):