update 对dapper查询页面的返回进行了优化
This commit is contained in:
@@ -51,26 +51,78 @@ namespace Ewide.Core.Extension
|
||||
return source.OrderBy(OrderBuilder<T>(input)).Select(u => u.Adapt<O>()).ToPagedListAsync(input.PageNo, input.PageSize);
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<dynamic>> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
|
||||
#region DAPPER
|
||||
public async static Task<PagedList> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
|
||||
{
|
||||
return source.QueryAsync(
|
||||
var count = await source.PageTotalCount(
|
||||
sql,
|
||||
param: param,
|
||||
transaction: transaction,
|
||||
commandTimeout: commandTimeout,
|
||||
commandType: commandType
|
||||
);
|
||||
|
||||
var data = await source.QueryAsync(
|
||||
PageSqlBuilder(sql, input),
|
||||
param: param,
|
||||
transaction: transaction,
|
||||
commandTimeout: commandTimeout,
|
||||
commandType: commandType
|
||||
);
|
||||
|
||||
var page = new PagedList
|
||||
{
|
||||
PageIndex = input.PageNo,
|
||||
PageSize = input.PageSize,
|
||||
Items = data,
|
||||
TotalCount = count,
|
||||
TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
};
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<T>> QueryPageData<T>(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
|
||||
public async static Task<PagedList<T>> QueryPageData<T>(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) where T : new()
|
||||
{
|
||||
return source.QueryAsync<T>(
|
||||
var count = await source.PageTotalCount(
|
||||
sql,
|
||||
param: param,
|
||||
transaction: transaction,
|
||||
commandTimeout: commandTimeout,
|
||||
commandType: commandType
|
||||
);
|
||||
|
||||
var data = await source.QueryAsync<T>(
|
||||
PageSqlBuilder(sql, input),
|
||||
param: param,
|
||||
transaction: transaction,
|
||||
commandTimeout: commandTimeout,
|
||||
commandType: commandType
|
||||
);
|
||||
|
||||
var page = new PagedList<T>
|
||||
{
|
||||
PageIndex = input.PageNo,
|
||||
PageSize = input.PageSize,
|
||||
Items = data,
|
||||
TotalCount = count,
|
||||
TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
};
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
private async static Task<int> PageTotalCount(this IDapperRepository source, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
|
||||
{
|
||||
var countSql = String.Format("SELECT COUNT(0) FROM ({0}) T", sql);
|
||||
var countAsync = await source.QueryAsync<int>(
|
||||
countSql,
|
||||
param: param,
|
||||
transaction: transaction,
|
||||
commandTimeout: commandTimeout,
|
||||
commandType: commandType);
|
||||
var count = countAsync.SingleOrDefault();
|
||||
return count;
|
||||
}
|
||||
|
||||
private static string PageSqlBuilder(string sql , PageInputBase input)
|
||||
@@ -83,5 +135,6 @@ namespace Ewide.Core.Extension
|
||||
sql += String.Join("", sqlStrList);
|
||||
return sql;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user