/// <summary> /// 应用排序 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this">The this.</param> /// <param name="orderBy">The order by.</param> /// <param name="mappingDictionary">The mapping dictionary.</param> public static ISelect <T> ApplySort <T>(this ISelect <T> @this, string orderBy, System.Collections.Generic.Dictionary <string, Core.Mapping.PropertyMappingValue> mappingDictionary) where T : class { Guards.ThrowIfNull(@this); Guards.ThrowIfNull(mappingDictionary); if (orderBy.IsNullOrWhiteSpace()) { return(@this); } var orderBySplited = orderBy.Split(","); orderBySplited.Reverse(); foreach (var orderByClause in orderBySplited) { var orderByClauseTrimmed = orderByClause.Trim(); var orderDescending = orderByClauseTrimmed.EndsWith(" desc"); // 是否倒序 var indexOfFirstSpace = orderByClauseTrimmed.IndexOf(" "); var propertyName = indexOfFirstSpace == -1 ? orderByClauseTrimmed : orderByClauseTrimmed.Remove(indexOfFirstSpace); if (!mappingDictionary.ContainsKey(propertyName)) { //throw new Exception($"未找到Key为{propertyName}的映射"); @this = @this.OrderBy(propertyName.ToSnakeCase() + (orderDescending ? " desc" : "")); continue; } var propertyMappingValue = mappingDictionary[propertyName]; Guards.ThrowIfNull(propertyMappingValue); foreach (var destinationProperty in propertyMappingValue.DestinationProperties.Reverse()) { if (propertyMappingValue.Revert) { orderDescending = !orderDescending; } @this = @this.OrderBy(destinationProperty.ToSnakeCase() + (orderDescending ? " desc" : "")); } } return(@this); }
protected virtual Page <TEntity> ToPage(ISelect <TEntity> condition, int pageIndex, int pageSize, string orderBy) { // 获取总数,分页数据有先后执行顺序要求 // Count前不允许使用OrderBy var total = condition.Count(); var page = condition .OrderBy(!string.IsNullOrWhiteSpace(orderBy), orderBy) .Page(pageIndex, pageSize).ToList(); return(new Page <TEntity> { Items = page, TotalItems = total }); }
protected virtual ISelect <TEntity> ApplySorting(ISelect <TEntity> query, TGetListInput input) { if (input is ISortedResultRequest sortInput) { if (!string.IsNullOrWhiteSpace(sortInput.Sorting)) { return(query.OrderBy(sortInput.Sorting)); } } if (input is ILimitedResultRequest) { return(query.OrderByDescending(e => e.Id)); } return(query); }
/// <summary> /// Should apply sorting if needed. /// </summary> /// <param name="query">The query.</param> /// <param name="input">The input.</param> protected virtual ISelect <TEntity> ApplySorting(ISelect <TEntity> query, TGetListInput input) { //Try to sort query if available if (input is ISortedResultRequest sortInput) { if (!sortInput.Sorting.IsNullOrWhiteSpace()) { return(query.OrderBy(sortInput.Sorting)); } } //IQueryable.Task requires sorting, so we should sort if Take will be used. if (input is ILimitedResultRequest) { return(query.OrderByDescending(e => e.Id)); } //No sorting return(query); }
/// <summary> /// 【linq to sql】专用扩展方法,不建议直接使用 /// </summary> public static ISelect <T1> ThenBy <T1, TMember>(this ISelect <T1> that, Expression <Func <T1, TMember> > column) where T1 : class => that.OrderBy(column);