Exemplo n.º 1
0
        public static IQueryable <T> ApplySorting <T>(this IQueryable <T> query, Filter filter)
        {
            if (filter.Sortings.Any())
            {
                PropertySorting propertySorting = filter.Sortings[0];
                query = query.OrderBy(propertySorting.PropertyName, propertySorting.IsAscending);
            }

            return(filter.Sortings
                   .Skip(1)
                   .Aggregate(query, (queryable, sorting) => queryable.ThenBy(sorting.PropertyName, sorting.IsAscending)));
        }
Exemplo n.º 2
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            ValueProviderResult skip    = bindingContext.ValueProvider.GetValue("skip");
            ValueProviderResult take    = bindingContext.ValueProvider.GetValue("take");
            ValueProviderResult orderBy = bindingContext.ValueProvider.GetValue("order_by");

            if (int.TryParse(skip.FirstValue, out int skipValue) == false)
            {
                return(Task.FromResult(false));
            }

            if (int.TryParse(take.FirstValue, out int takeValue) == false)
            {
                return(Task.FromResult(false));
            }

            var             sorting         = orderBy.FirstValue?.Split(",") ?? Array.Empty <string>();
            PropertySorting propertySorting = null;

            if (sorting.Length == 2)
            {
                string propertyName = sorting[0];
                bool   isAsc;
                if (sorting[1] == "asc")
                {
                    isAsc = true;
                }
                else if (sorting[1] == "desc")
                {
                    isAsc = false;
                }
                else
                {
                    return(Task.FromResult(false));
                }
                propertySorting = new PropertySorting(propertyName, isAsc);
            }

            bindingContext.Result = ModelBindingResult.Success(new Filter(
                                                                   skipValue,
                                                                   takeValue,
                                                                   propertySorting != null ? new[] { propertySorting } : null)
                                                               );
            return(Task.FromResult(true));
        }