NHibernate:如何表达特定的“group by”带条件查询

发布于 2024-09-10 11:47:50 字数 1622 浏览 4 评论 0原文

问题

可以生成以下查询的标准/预测是什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-09-17 11:47:50

这应该有效(尽管您可能需要 SUBSTRING(Name, 1, 1) 但无论如何):

Projections.SqlGroupProjection(
    "SUBSTRING(Name, 0, 1) as FirstChar",
    "SUBSTRING(Name, 0, 1)",
    new [] {"FirstChar"}, 
    new[] {NHibernateUtil.String}
)

This should work (although you might want SUBSTRING(Name, 1, 1) but anyway):

Projections.SqlGroupProjection(
    "SUBSTRING(Name, 0, 1) as FirstChar",
    "SUBSTRING(Name, 0, 1)",
    new [] {"FirstChar"}, 
    new[] {NHibernateUtil.String}
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文