- 优化 ISelect.Count() 之前使用了 OrderBy 会产生的 SQL 语法问题;

This commit is contained in:
28810 2019-12-19 15:50:42 +08:00
parent ec25ccea86
commit 969e690682
3 changed files with 32 additions and 4 deletions

View File

@ -209,8 +209,11 @@ namespace FreeSql.Tests.SqlServer
public void Count()
{
var count = select.Where(a => 1 == 1).Count();
var count11 = select.Where(a => 1 == 1).OrderBy(a => a.Id).Count();
select.Where(a => 1 == 1).Count(out var count2);
select.Where(a => 1 == 1).OrderBy(a => a.Id).Count(out var count22);
Assert.Equal(count, count2);
Assert.Equal(count11, count22);
Assert.Equal(0, select.Where(a => 1 == 2).Count());
var subquery = select.ToSql(a => new

View File

@ -762,8 +762,9 @@ namespace FreeSql.Internal
else if (fsqlType != null)
{
var call3Exp = exp3tmp as MethodCallExpression;
var method = fsqlType.GetMethod(call3Exp.Method.Name, call3Exp.Arguments.Select(a => a.Type).ToArray());
if (call3Exp.Method.ContainsGenericParameters) method.MakeGenericMethod(call3Exp.Method.GetGenericArguments());
var method = call3Exp.Method;
//var method = fsqlType.GetMethod(call3Exp.Method.Name, call3Exp.Arguments.Select(a => a.Type).ToArray());
//if (call3Exp.Method.ContainsGenericParameters) method.MakeGenericMethod(call3Exp.Method.GetGenericArguments());
var parms = method.GetParameters();
var args = new object[call3Exp.Arguments.Count];
for (var a = 0; a < args.Length; a++)

View File

@ -158,7 +158,19 @@ namespace FreeSql.Internal.CommonProvider
return this.ToList<int>("1 as1").Sum() > 0; //这里的 Sum 为了分表查询
}
public long Count() => this.ToList<int>("count(1) as1").Sum(); //这里的 Sum 为了分表查询
public long Count()
{
var tmpOrderBy = _orderby;
_orderby = null; //解决 select count(1) from t order by id 这样的 SQL 错误
try
{
return this.ToList<int>("count(1) as1").Sum(); //这里的 Sum 为了分表查询
}
finally
{
_orderby = tmpOrderBy;
}
}
public TSelect Count(out long count)
{
@ -1111,7 +1123,19 @@ namespace FreeSql.Internal.CommonProvider
return (await this.ToListAsync<int>("1 as1")).Sum() > 0; //这里的 Sum 为了分表查询
}
async public Task<long> CountAsync() => (await this.ToListAsync<int>("count(1) as1")).Sum(); //这里的 Sum 为了分表查询
async public Task<long> CountAsync()
{
var tmpOrderBy = _orderby;
_orderby = null;
try
{
return (await this.ToListAsync<int>("count(1) as1")).Sum(); //这里的 Sum 为了分表查询
}
finally
{
_orderby = tmpOrderBy;
}
}
async public Task<DataTable> ToDataTableAsync(string field = null)
{