@@ -47,32 +47,34 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||
}
|
||||
else
|
||||
{
|
||||
#region 单字段排序,前端不传递,后端给排序字段
|
||||
//var propName = string.Empty;
|
||||
|
||||
var propName = string.Empty;
|
||||
//if (string.IsNullOrWhiteSpace(pageInput.SortField))
|
||||
//{
|
||||
// //没有指定,优先以Id排序,否则从属性里面随便取出来一个排序
|
||||
// var propertyNameList = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(t => t.CanWrite).Select(t => t.Name).OrderBy(t => t).ToList();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(pageInput.SortField))
|
||||
{
|
||||
//没有指定,优先以Id排序,否则从属性里面随便取出来一个排序
|
||||
var propertyNameList = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(t => t.CanWrite).Select(t => t.Name).OrderBy(t => t).ToList();
|
||||
// if (propertyNameList.Count == 0)
|
||||
// {
|
||||
// throw new InvalidOperationException("no default sort field.");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// propName = propertyNameList.Contains("Id") ? "Id" : propertyNameList.FirstOrDefault();
|
||||
// }
|
||||
|
||||
if (propertyNameList.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("no default sort field.");
|
||||
}
|
||||
else
|
||||
{
|
||||
propName = propertyNameList.Contains("Id") ? "Id" : propertyNameList.FirstOrDefault();
|
||||
}
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// //有值,以前段传输的为主
|
||||
// propName = pageInput.SortField;
|
||||
//}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//有值,以前段传输的为主
|
||||
propName = pageInput.SortField;
|
||||
}
|
||||
|
||||
source = pageInput.Asc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
|
||||
//source = pageInput.Asc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
|
||||
#endregion
|
||||
|
||||
source = string.IsNullOrWhiteSpace(pageInput.SortField) ? source : (pageInput.Asc ? source.OrderBy(pageInput.SortField) : source.OrderBy(pageInput.SortField + " desc"));
|
||||
}
|
||||
|
||||
source = source.Skip((pageInput.PageIndex - 1) * pageInput.PageSize);
|
||||
@@ -93,54 +95,46 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||
}
|
||||
|
||||
|
||||
|
||||
//单字段排序 异步
|
||||
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, int pageNumber, int pageSize, string defaultSortFiled = "Id", bool isAsc = true, bool isMultiSortFiled = false, string[] sortArray = default)
|
||||
//兼容之前后端给了默认值的,不影响之前的逻辑,只是改写法
|
||||
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, PageInput pageInput, string sortField , CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
if (isMultiSortFiled && sortArray == default)
|
||||
|
||||
if (pageInput.PageIndex <= 0)
|
||||
{
|
||||
throw new System.Exception("The sort field must be specified");
|
||||
pageInput.PageIndex = 1;
|
||||
}
|
||||
if (pageInput.PageSize <= 0)
|
||||
{
|
||||
pageInput.PageSize = 10;
|
||||
}
|
||||
|
||||
if (pageNumber <= 0)
|
||||
{
|
||||
pageNumber = 1;
|
||||
}
|
||||
if (pageSize <= 0)
|
||||
{
|
||||
pageSize = 10;
|
||||
}
|
||||
|
||||
var count = await source.CountAsync().ConfigureAwait(false);
|
||||
var count = await source.CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
return new PageOutput<T>() { CurrentPageData = new T[0] };
|
||||
}
|
||||
var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
|
||||
|
||||
if (!isMultiSortFiled)
|
||||
var propName = string.Empty;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(pageInput.SortField))
|
||||
{
|
||||
source = isAsc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
|
||||
|
||||
propName = sortField;
|
||||
}
|
||||
else
|
||||
{
|
||||
var sortString = string.Join(',', sortArray);
|
||||
|
||||
source = source.OrderBy(sortString);
|
||||
}
|
||||
source = source.Skip((pageNumber - 1) * pageSize);
|
||||
source = string.IsNullOrWhiteSpace(propName) ? source : (pageInput.Asc ? source.OrderBy(propName) : source.OrderBy(propName + " desc"));
|
||||
|
||||
source = source.Skip((pageInput.PageIndex - 1) * pageInput.PageSize);
|
||||
var items = await source
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync()
|
||||
.Take(pageInput.PageSize)
|
||||
.ToArrayAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var pagedList = new PageOutput<T>()
|
||||
{
|
||||
PageIndex = pageNumber,
|
||||
PageSize = pageSize,
|
||||
PageIndex = pageInput.PageIndex,
|
||||
PageSize = pageInput.PageSize,
|
||||
TotalCount = count,
|
||||
CurrentPageData = items
|
||||
};
|
||||
@@ -149,127 +143,6 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||
}
|
||||
|
||||
|
||||
#region 同步方法废弃
|
||||
////单字段排序
|
||||
//public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
|
||||
//{
|
||||
// if (pageIndex <= 0)
|
||||
// {
|
||||
// pageIndex = 1;
|
||||
// }
|
||||
// if (pageSize <= 0)
|
||||
// {
|
||||
// pageSize = 10;
|
||||
// }
|
||||
// var count = source.Count();
|
||||
|
||||
// if (count == 0)
|
||||
// {
|
||||
// return new PageOutput<T>() { CurrentPageData = new T[0] };
|
||||
// }
|
||||
|
||||
|
||||
// var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
|
||||
|
||||
// source = isAsc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
|
||||
|
||||
// source = source.Skip((pageIndex - 1) * pageSize);
|
||||
|
||||
// var items = source
|
||||
// .Take(pageSize)
|
||||
// .ToArray();
|
||||
|
||||
// var pagedList = new PageOutput<T>()
|
||||
// {
|
||||
// PageIndex = pageIndex,
|
||||
// PageSize = pageSize,
|
||||
// TotalCount = count,
|
||||
// CurrentPageData = items
|
||||
// };
|
||||
|
||||
// return pagedList;
|
||||
//}
|
||||
|
||||
//public static PageOutput<T> ToPagedList<T>(this IList<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
|
||||
//{
|
||||
// if (pageIndex <= 0)
|
||||
// {
|
||||
// pageIndex = 1;
|
||||
// }
|
||||
// if (pageSize <= 0)
|
||||
// {
|
||||
// pageSize = 10;
|
||||
// }
|
||||
// var count = source.Count();
|
||||
|
||||
// if (count == 0)
|
||||
// {
|
||||
// return new PageOutput<T>() { CurrentPageData = new List<T>() };
|
||||
// }
|
||||
|
||||
// var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
|
||||
|
||||
// IQueryable<T> sourceQuery = isAsc ? source.AsQueryable().OrderBy(propName) : source.AsQueryable().OrderBy(propName + " desc");
|
||||
|
||||
// sourceQuery = sourceQuery.Skip((pageIndex - 1) * pageSize);
|
||||
|
||||
// var items = sourceQuery
|
||||
// .Take(pageSize)
|
||||
// .ToArray();
|
||||
|
||||
// var pagedList = new PageOutput<T>()
|
||||
// {
|
||||
// PageIndex = pageIndex,
|
||||
// PageSize = pageSize,
|
||||
// TotalCount = count,
|
||||
// CurrentPageData = items
|
||||
// };
|
||||
|
||||
// return pagedList;
|
||||
//}
|
||||
|
||||
|
||||
////多字段排序 ["a asc", "b desc", "c asc"]
|
||||
//public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string[] sortArray)
|
||||
//{
|
||||
// if (pageIndex <= 0)
|
||||
// {
|
||||
// pageIndex = 1;
|
||||
// }
|
||||
// if (pageSize <= 0)
|
||||
// {
|
||||
// pageSize = 10;
|
||||
// }
|
||||
// var count = source.Count();
|
||||
|
||||
// if (count == 0)
|
||||
// {
|
||||
// return new PageOutput<T>() { CurrentPageData = new T[0] };
|
||||
// }
|
||||
|
||||
// var sortString = string.Join(',', sortArray);
|
||||
|
||||
// source.OrderBy(sortString);
|
||||
|
||||
// source = source.Skip((pageIndex - 1) * pageSize);
|
||||
|
||||
// var items = source
|
||||
// .Take(pageSize)
|
||||
// .ToArray();
|
||||
|
||||
// var pagedList = new PageOutput<T>()
|
||||
// {
|
||||
// PageIndex = pageIndex,
|
||||
// PageSize = pageSize,
|
||||
// TotalCount = count,
|
||||
// CurrentPageData = items
|
||||
// };
|
||||
|
||||
// return pagedList;
|
||||
//}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user