- 优化 FreeSql.Extensions.Linq Select 选定字段查询方法;#674 #533

This commit is contained in:
2881099 2021-02-02 13:07:01 +08:00
parent ef4cf46556
commit 56d2128726
3 changed files with 27 additions and 2 deletions

View File

@ -24,6 +24,12 @@ namespace FreeSql.Tests
Assert.Equal(@"SELECT count(distinct a.""status"") as1 Assert.Equal(@"SELECT count(distinct a.""status"") as1
FROM ""ts_up_dywhere01"" a", sql); FROM ""ts_up_dywhere01"" a", sql);
sql = fsql.Select<ts_up_dywhere01>().Select(a => new { a.status }).Distinct().ToSql();
fsql.Select<ts_up_dywhere01>().Select(a => new { a.status }).Distinct().Count(out count);
Assert.Equal(@"SELECT DISTINCT a.""status"" as1
FROM ""ts_up_dywhere01"" a", sql);
} }
[Fact] [Fact]

View File

@ -698,18 +698,23 @@ namespace FreeSql.Internal.CommonProvider
var tmpOrderBy = _orderby; var tmpOrderBy = _orderby;
var tmpSkip = _skip; var tmpSkip = _skip;
var tmpLimit = _limit; var tmpLimit = _limit;
var tmpDistinct = _distinct;
_orderby = null; //解决 select count(1) from t order by id 这样的 SQL 错误 _orderby = null; //解决 select count(1) from t order by id 这样的 SQL 错误
_skip = 0; _skip = 0;
_limit = 0; _limit = 0;
_distinct = false;
try try
{ {
return this.ToList<int>($"count(1){_commonUtils.FieldAsAlias("as1")}").Sum(); //这里的 Sum 为了分表查询 var countField = "1";
if (tmpDistinct && _selectExpression != null) countField = $"distinct {this.GetExpressionField(_selectExpression, FieldAliasOptions.AsProperty).field}";
return this.ToList<int>($"count({countField}){_commonUtils.FieldAsAlias("as1")}").Sum(); //这里的 Sum 为了分表查询
} }
finally finally
{ {
_orderby = tmpOrderBy; _orderby = tmpOrderBy;
_skip = tmpSkip; _skip = tmpSkip;
_limit = tmpLimit; _limit = tmpLimit;
_distinct = tmpDistinct;
} }
} }
public TSelect Count(out long count) public TSelect Count(out long count)
@ -741,18 +746,23 @@ namespace FreeSql.Internal.CommonProvider
var tmpOrderBy = _orderby; var tmpOrderBy = _orderby;
var tmpSkip = _skip; var tmpSkip = _skip;
var tmpLimit = _limit; var tmpLimit = _limit;
var tmpDistinct = _distinct;
_orderby = null; _orderby = null;
_skip = 0; _skip = 0;
_limit = 0; _limit = 0;
_distinct = false;
try try
{ {
return (await this.ToListAsync<int>($"count(1){_commonUtils.FieldAsAlias("as1")}", cancellationToken)).Sum(); //这里的 Sum 为了分表查询 var countField = "1";
if (tmpDistinct && _selectExpression != null) countField = $"distinct {this.GetExpressionField(_selectExpression, FieldAliasOptions.AsProperty).field}";
return (await this.ToListAsync<int>($"count({countField}){_commonUtils.FieldAsAlias("as1")}", cancellationToken)).Sum(); //这里的 Sum 为了分表查询
} }
finally finally
{ {
_orderby = tmpOrderBy; _orderby = tmpOrderBy;
_skip = tmpSkip; _skip = tmpSkip;
_limit = tmpLimit; _limit = tmpLimit;
_distinct = tmpDistinct;
} }
} }
public virtual Task<List<T1>> ToListAsync(bool includeNestedMembers = false, CancellationToken cancellationToken = default) public virtual Task<List<T1>> ToListAsync(bool includeNestedMembers = false, CancellationToken cancellationToken = default)

View File

@ -516,6 +516,15 @@ namespace FreeSql.Internal.CommonProvider
static EventHandler<Aop.AuditDataReaderEventArgs> _OldAuditDataReaderHandler; static EventHandler<Aop.AuditDataReaderEventArgs> _OldAuditDataReaderHandler;
public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2() public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2()
{ {
if (_selectExpression != null) //ToSql
{
var af = this.GetExpressionField(_selectExpression);
return new GetAllFieldExpressionTreeInfo
{
Field = af.field,
Read = (dr, idx) => throw new Exception("GetAllFieldExpressionTreeInfo.Read Is Null")
};
}
if (_OldAuditDataReaderHandler != _orm.Aop.AuditDataReaderHandler) if (_OldAuditDataReaderHandler != _orm.Aop.AuditDataReaderHandler)
{ {
_OldAuditDataReaderHandler = _orm.Aop.AuditDataReaderHandler; //清除单表 ExppressionTree _OldAuditDataReaderHandler = _orm.Aop.AuditDataReaderHandler; //清除单表 ExppressionTree