c# - NHibernate SQLFunction 分组依据

标签 c# mysql nhibernate group-by queryover

我正在尝试做这样的事情:

SELECT round(song.rating), count(song.song_id) FROM song
GROUP BY round(song.rating);

我的查询:

 var output = sess.QueryOver<song>()
                .SelectList(list => list
                    .Select(Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty("rating")))
                    .SelectCount(s => s.song_id))
                .List<object[]>()
                .Select(prop => new RatingStat
                {
                    rating = (int)prop[0],
                    count = (int)prop[1]
                }).ToList<RatingStat>();

预期输出:

+---------------------------+---------------------------+
|                         0 |                        12 |
|                         1 |                         1 |
|                         3 |                         1 |
|                         4 |                         6 |
|                         5 |                         3 |
|                         6 |                         6 |
|                         7 |                        12 |
|                         8 |                         7 |
|                         9 |                         9 |
|                        10 |                         2 |
+---------------------------+---------------------------+

实际输出:

0                         12
1                         1
3                         1
4                         1
4                         3
4                         1
4                         1
5                         1
5                         1
5                         1
6                         2
6                         1
6                         3
7                         2
7                         9
7                         1
8                         1
8                         3
8                         2
8                         1
9                         1
9                         3
9                         1
9                         4
10                        2

我正在使用从 MySQL5Dialect 继承的我自己的方言,因为我的 MySQL 方言不支持 round 函数。 以下是在我的方言中如何定义 round 函数:

 RegisterFunction("round", new StandardSafeSQLFunction("round", NHibernateUtil.Int32,1));

我的问题是,为什么我有多个评分值相同的组?四舍五入的值应该是不同的。圆形功能是否可能无法正常工作? 编辑:添加生成的 SQL 语句

 SELECT round(this_.rating) as y0_, count(this_.song_ID) as y1_ FROM song this_ GROUP BY this_.rating

最佳答案

找到的解决方案:

 var t = Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty("rating"));
 var output = sess.QueryOver<Song>()
       .SelectList(list => list
       .Select(Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty(t)))
       .SelectCount(s => s.song_id))
       .List<object[]>()
       .Select(prop => new RatingStat
        {
            rating = (int)prop[0],
            count = (int)prop[1]
        }).ToList<RatingStat>();

关于c# - NHibernate SQLFunction 分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330468/

相关文章:

c# - 将 winapi 事件转发到另一个窗口

C# 将 1D 数组拆分为每个第 N 个值的 2D 数组

mysql - MySQL 可以将字符串转换为表达式吗?

nhibernate - 在带有导入类的 hbm 中使用命名查询

c# - 在 Entity Framework 中使用 linq 提供多个 List<int> 类型标准

c# - 可以快速调整大小的数组

php - Laravel Eloquent created_at 正在更新

Mysql:从字符串中提取子字符串

nhibernate - 为什么 Linq to Nhibernate 会产生外连接

nhibernate - 为什么许多 nHibernate 示例代码实例都包含 CaSTLe dll?