mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 优化 FreeSql.Extensions.Linq Select 选定字段查询方法;#674 #533
This commit is contained in:
		@@ -24,6 +24,12 @@ namespace FreeSql.Tests
 | 
			
		||||
 | 
			
		||||
            Assert.Equal(@"SELECT count(distinct a.""status"") as1 
 | 
			
		||||
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]
 | 
			
		||||
 
 | 
			
		||||
@@ -698,18 +698,23 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
            var tmpOrderBy = _orderby;
 | 
			
		||||
            var tmpSkip = _skip;
 | 
			
		||||
            var tmpLimit = _limit;
 | 
			
		||||
            var tmpDistinct = _distinct;
 | 
			
		||||
            _orderby = null; //解决 select count(1) from t order by id 这样的 SQL 错误
 | 
			
		||||
            _skip = 0;
 | 
			
		||||
            _limit = 0;
 | 
			
		||||
            _distinct = false;
 | 
			
		||||
            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
 | 
			
		||||
            {
 | 
			
		||||
                _orderby = tmpOrderBy;
 | 
			
		||||
                _skip = tmpSkip;
 | 
			
		||||
                _limit = tmpLimit;
 | 
			
		||||
                _distinct = tmpDistinct;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        public TSelect Count(out long count)
 | 
			
		||||
@@ -741,18 +746,23 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
            var tmpOrderBy = _orderby;
 | 
			
		||||
            var tmpSkip = _skip;
 | 
			
		||||
            var tmpLimit = _limit;
 | 
			
		||||
            var tmpDistinct = _distinct;
 | 
			
		||||
            _orderby = null;
 | 
			
		||||
            _skip = 0;
 | 
			
		||||
            _limit = 0;
 | 
			
		||||
            _distinct = false;
 | 
			
		||||
            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
 | 
			
		||||
            {
 | 
			
		||||
                _orderby = tmpOrderBy;
 | 
			
		||||
                _skip = tmpSkip;
 | 
			
		||||
                _limit = tmpLimit;
 | 
			
		||||
                _distinct = tmpDistinct;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        public virtual Task<List<T1>> ToListAsync(bool includeNestedMembers = false, CancellationToken cancellationToken = default)
 | 
			
		||||
 
 | 
			
		||||
@@ -516,6 +516,15 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
        static EventHandler<Aop.AuditDataReaderEventArgs> _OldAuditDataReaderHandler;
 | 
			
		||||
        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)
 | 
			
		||||
            {
 | 
			
		||||
                _OldAuditDataReaderHandler = _orm.Aop.AuditDataReaderHandler; //清除单表 ExppressionTree
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user