零时方案修改

This commit is contained in:
2023-10-13 17:16:06 +08:00
parent e6a909bba0
commit be3dde7657
4 changed files with 115 additions and 53 deletions
@@ -3,6 +3,8 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Collections.Generic;
using System;
namespace IRaCIS.Core.Infrastructure.Extention
{
@@ -23,7 +25,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
if (count == 0)
{
return new PageOutput<T>() { CurrentPageData=new T[0] };
return new PageOutput<T>() { CurrentPageData = new T[0] };
}
@@ -49,10 +51,10 @@ 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, CancellationToken cancellationToken = default)
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, CancellationToken cancellationToken = default)
{
if (isMultiSortFiled&& sortArray==default)
if (isMultiSortFiled && sortArray == default)
{
throw new System.Exception("The sort field must be specified");
}
@@ -83,7 +85,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
{
var sortString = string.Join(',', sortArray);
source= source.OrderBy(sortString);
source = source.OrderBy(sortString);
}
source = source.Skip((pageNumber - 1) * pageSize);
var items = await source
@@ -102,8 +104,47 @@ namespace IRaCIS.Core.Infrastructure.Extention
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)
public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string[] sortArray)
{
if (pageIndex <= 0)
{
@@ -180,5 +221,8 @@ namespace IRaCIS.Core.Infrastructure.Extention
return pagedList;
}
}
}