dotnetcore / dotnetcore/FreeSql

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

Open
#2,207 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

#### 问题描述及重现代码:

```c#
var pages = await freeSql.Select()
.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为:
```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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.