diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 1a782b08d..08284c977 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1044,6 +1044,14 @@ namespace SqlSugar } public virtual int Count() { + if (this.QueryBuilder.Skip == null&& + this.QueryBuilder.Take == null&& + this.QueryBuilder.OrderByValue == null && + this.QueryBuilder.PartitionByValue == null) + { + + return this.Clone().Select(" COUNT(1) ").ToList().First(); + } MappingTableList expMapping; int result; _CountBegin(out expMapping, out result); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs index ad49f38c4..90ce43193 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs @@ -47,7 +47,9 @@ namespace SqlSugar if (this.updateobj.UpdateBuilder.DbColumnInfoList.Any()) { var columns=this.updateobj.UpdateBuilder.DbColumnInfoList.Select(it => it.DbColumnName).Distinct().ToList(); - return this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(x => !columns.Any(y => y.EqualCase(x.DbColumnName))).Select(it => it.DbColumnName).ToArray(); + var result= this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(x => !columns.Any(y => y.EqualCase(x.DbColumnName))).Select(it => it.DbColumnName).ToArray(); + result = result.Where(it => !string.IsNullOrEmpty(it)).ToArray(); + return result; } else { diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs index 7da3dc06f..f049822fa 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerQueryBuilder.cs @@ -52,21 +52,25 @@ namespace SqlSugar var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty(); AppendFilter(); sql = new StringBuilder(); + var oldOrderByValue = this.OrderByValue; if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() "; if (this.PartitionByValue.HasValue()) { this.OrderByValue = this.PartitionByValue + this.OrderByValue; } var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false; - var isRowNumber = (Skip != null || Take != null) && !isFirst; + var isTop = (Skip == null && Take != null && DisableTop == false); + var isRowNumber = (Skip != null || Take != null) && !isFirst && !isTop; + if (!isRowNumber && oldOrderByValue == null) { this.OrderByValue = null; } + if (isFirst && oldOrderByValue == "ORDER BY GETDATE() ") { this.OrderByValue = null; } var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString); string groupByValue = GetGroupByString + HavingInfos; string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null; if (isIgnoreOrderBy) { orderByValue = null; } - sql.AppendFormat(SqlTemplate, isFirst ? (" TOP 1 " + GetSelectValue) : GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue); + sql.AppendFormat(SqlTemplate, GetSelect(isFirst,isTop), base.GetTableNameString, base.GetWhereValueString, groupByValue, orderByValue); sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null); if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); } - var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip); + var result = (isFirst || isTop) ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip); if (ExternalPageIndex > 0) { if (externalOrderBy.IsNullOrEmpty()) @@ -85,10 +89,10 @@ namespace SqlSugar if (this.OldSql.HasValue()) this.OldSql += " ORDER BY GETDATE() "; } - else + else { if (this.OldSql.HasValue()) - this.OldSql += (" "+this.GetOrderByString); + this.OldSql += (" " + this.GetOrderByString); } result += this.Offset; @@ -98,5 +102,21 @@ namespace SqlSugar result = GetSqlQuerySql(result); return result; } + + private string GetSelect(bool isFirst,bool isTop) + { + if (isFirst) + { + return (" TOP 1 " + GetSelectValue); + } + else if(isTop) + { + return ($" TOP {this.Take} " + GetSelectValue); + } + else + { + return GetSelectValue; + } + } } }