Skip to content

MSSQL下GroupBy后Count(out)语法错误 #2207

@Hsuwen

Description

@Hsuwen

问题描述及重现代码:

var pages = await freeSql.Select<TransactionIndex>()
    .WithTransaction(transaction)
    .WhereIf(!string.IsNullOrEmpty(groupId), s => s.GroupId == groupId)
    .WhereIf(string.IsNullOrEmpty(groupId), s => s.GroupId != null)
    .GroupBy(s => s.GroupId)
    .OrderBy(s => s.Key)
    .Count(out long totalCount)
    .Page(pageIndex, pageSize)
    .ToListAsync(s => new
    {
        GroupId = s.Key!,
        USDAmount = s.Sum(s.Value.USDAmount),
        TransactionTotal = s.Count(s.Value.TxnId),
    });

其中.Count(out long totalCount)生成的SQL为:

select count(1) from (SELECT 1 as1
FROM [trans_index] a
WHERE (a.[GroupId]  IS NOT  NULL)
GROUP BY a.[GroupId]
ORDER BY a.[GroupId]) fta

运行异常:The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
此处不应该存在ORDER BY语句。

数据库版本

Microsoft SQL Server 2022

安装的Nuget包

FreeSql 3.5.306
FreeSql.Repository 3.5.306
FreeSql.Provider.SqlServer 3.5.306

.net framework/. net core? 及具体版本

.net 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions