public static IQueryable <TQuery> ApplySorting <TQuery>(this ISupportSorting sortingProvider, IQueryable <TQuery> query, Func <IQueryable <TQuery>, IOrderedQueryable <TQuery> >?defaultSort = null) { if (sortingProvider.Sorting != null && sortingProvider.Sorting.Any()) { var parameterExpression = Expression.Parameter(typeof(TQuery), "x"); var sortedSortItems = sortingProvider.Sorting .OrderBy(x => x.Order.GetValueOrDefault(99)); IOrderedQueryable <TQuery>?ordered = null; foreach (var sortItem in sortedSortItems) { ordered = ordered.Append(query, sortItem, parameterExpression); } query = ordered ?? query; } else if (defaultSort != null) { query = defaultSort(query); } return(query); }