using Furion.LinqBuilder; using System.Linq.Expressions; namespace Admin.NET.Core { public static class QueryableExstenstions { /// /// 根据分页查询参数为IQueryable附加过滤条件 /// /// /// /// /// public static IQueryable Search(this IQueryable source, PageInputBase searchParameters) { if (searchParameters.SearchParameters == null) return source; var results = source.Where(LambdaExpressionBuilder.BuildLambda(searchParameters.SearchParameters)); //无排序字段 if (searchParameters.SortField.IsNullOrEmpty()) return results; return results.ApplyOrder(searchParameters.SortField, searchParameters.SortOrder); } /// /// 附加排序 /// /// /// /// /// /// private static IOrderedQueryable ApplyOrder(this IQueryable source, string property, string sortMethod) { var type = typeof(T); var parameterExp = Expression.Parameter(type, "x"); var propertyInfo = type.GetProperty(property); var propertyExp = Expression.Property(parameterExp, propertyInfo); var lambdaExp = Expression.Lambda>(propertyExp, parameterExp); return sortMethod == "descend" ? source.OrderByDescending(lambdaExp) : source.OrderBy(lambdaExp); } } }