using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic.Core;
using SqlSugar;
namespace DS.Module.Core.Extensions;
public static partial class Extensions
{
///
/// 多排序方法
///
/// 要排序实体
/// 源
/// 排序条件
///
public static ISugarQueryable OrderBy(this ISugarQueryable source,
SortCondition[] orderConditions)
{
orderConditions.NotNull(nameof(orderConditions));
string orderStr = string.Empty;
foreach (SortCondition orderCondition in orderConditions)
{
orderStr = orderStr +
$"{orderCondition.SortField} {(orderCondition.ListSortDirection == ListSortDirection.Ascending ? "asc" : "desc")}, ";
}
orderStr = orderStr.TrimEnd(", ".ToCharArray());
return source.OrderBy(orderStr);
}
///
///
///
///
///
///
///
public static DataResult> ToQueryPage(this ISugarQueryable source, PageCondition page)
{
page.NotNull(nameof(page));
var result = source.WhereAsync(page.PageIndex, page.PageSize, page.SortConditions);
var list = result.data;
var total = result.totalNumber;
return DataResult>.PageList(total, list);
}
///
///
///
///
///
///
///
///
///
private static (List data, int totalNumber) WhereAsync(this ISugarQueryable source,
int pageIndex,
int pageSize, SortCondition[] orderConditions)
{
var total = source.Count();
ISugarQueryable orderSource;
if (orderConditions == null || orderConditions.Length == 0)
{
// orderSource = source.OrderBy("Id ascending");
orderSource = source.OrderBy("Id");
// orderSource = source.OrderBy("GID");
}
else
{
orderSource = source.OrderBy(orderConditions);
}
source = orderSource;
return (
source.Count()!=0
? source.ToPageList(pageIndex,pageSize,ref total)
: Enumerable.Empty().ToList(), total);
}
}