Code optimization

This commit is contained in:
sunkaixuan 2019-05-20 13:49:17 +08:00
parent f6dd14c66b
commit 4964904f2f

View File

@ -596,14 +596,9 @@ namespace SqlSugar
} }
public virtual int Count() public virtual int Count()
{ {
MappingTableList expMapping = new MappingTableList(); MappingTableList expMapping;
if (QueryBuilder.EntityName== "ExpandoObject" && this.Context.MappingTables.Any(it => it.EntityName == "ExpandoObject")) int result;
{ _CountBegin(out expMapping, out result);
expMapping.Add("ExpandoObject", this.Context.MappingTables.First(it => it.EntityName == "ExpandoObject").DbTableName);
}
InitMapping();
QueryBuilder.IsCount = true;
int result = 0;
if (IsCache) if (IsCache)
{ {
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService; var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
@ -613,16 +608,7 @@ namespace SqlSugar
{ {
result = GetCount(); result = GetCount();
} }
RestoreMapping(); _CountEnd(expMapping);
QueryBuilder.IsCount = false;
if (expMapping.Count > 0)
{
if (this.QueryableMappingTableList == null)
{
this.QueryableMappingTableList = new MappingTableList();
}
this.QueryableMappingTableList.Add(expMapping.First());
}
return result; return result;
} }
@ -924,9 +910,9 @@ namespace SqlSugar
public async Task<int> CountAsync() public async Task<int> CountAsync()
{ {
InitMapping(); MappingTableList expMapping;
QueryBuilder.IsCount = true; int result;
int result = 0; _CountBegin(out expMapping, out result);
if (IsCache) if (IsCache)
{ {
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService; var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
@ -936,8 +922,7 @@ namespace SqlSugar
{ {
result =await GetCountAsync(); result =await GetCountAsync();
} }
RestoreMapping(); _CountEnd(expMapping);
QueryBuilder.IsCount = false;
return result; return result;
} }
public async Task<int> CountAsync(Expression<Func<T, bool>> expression) public async Task<int> CountAsync(Expression<Func<T, bool>> expression)
@ -1066,11 +1051,35 @@ namespace SqlSugar
totalNumber.Value = await this.Clone().CountAsync(); totalNumber.Value = await this.Clone().CountAsync();
return await this.Clone().ToDataTablePageAsync(pageIndex, pageSize); return await this.Clone().ToDataTablePageAsync(pageIndex, pageSize);
} }
#endregion #endregion
#region Private Methods #region Private Methods
private void _CountEnd(MappingTableList expMapping)
{
RestoreMapping();
QueryBuilder.IsCount = false;
if (expMapping.Count > 0)
{
if (this.QueryableMappingTableList == null)
{
this.QueryableMappingTableList = new MappingTableList();
}
this.QueryableMappingTableList.Add(expMapping.First());
}
}
private void _CountBegin(out MappingTableList expMapping, out int result)
{
expMapping = new MappingTableList();
if (QueryBuilder.EntityName == "ExpandoObject" && this.Context.MappingTables.Any(it => it.EntityName == "ExpandoObject"))
{
expMapping.Add("ExpandoObject", this.Context.MappingTables.First(it => it.EntityName == "ExpandoObject").DbTableName);
}
InitMapping();
QueryBuilder.IsCount = true;
result = 0;
}
protected ISugarQueryable<TResult> _Select<TResult>(Expression expression) protected ISugarQueryable<TResult> _Select<TResult>(Expression expression)
{ {
QueryBuilder.CheckExpression(expression, "Select"); QueryBuilder.CheckExpression(expression, "Select");