private static Expression <Func <T, bool> > GetExpression <T>(
            MappingCollection <T> mappings,
            FilterGroup filterGroup
            ) where T : class
        {
            var predicate     = PredicateBuilder.New <T>(filterGroup.Operator == FilterOperator.And);
            var filterApplied = false;

            foreach (var filter in filterGroup.Filters.Where(f => !string.IsNullOrWhiteSpace(f.Value)))
            {
                var mapping = mappings.GetMapping(filter.Field);

                var expression = mapping.GetFilterLambda(filter.Value, filter.Action);

                if (expression == null)
                {
                    continue;
                }

                filterApplied = true;

                if (filterGroup.Operator == FilterOperator.And)
                {
                    predicate = predicate.And(expression);
                }
                else
                {
                    predicate = predicate.Or(expression);
                }
            }

            // if an or filter doesnt actually do any filtering, we return true.
            // this allows us to ignore empty predicates
            if (!filterApplied && filterGroup.Operator == FilterOperator.Or)
            {
                predicate = PredicateBuilder.New <T>(true);
            }

            if (filterGroup.FilterGroups != null)
            {
                foreach (var group in filterGroup.FilterGroups)
                {
                    if (filterGroup.Operator == FilterOperator.And)
                    {
                        predicate = predicate.And(GetExpression <T>(mappings, group));
                    }
                    else
                    {
                        predicate = predicate.Or(GetExpression <T>(mappings, group));
                    }
                }
            }

            return(predicate);
        }
        private IQueryable <TEntityType> ApplySort <TEntityType>(
            IQueryable <TEntityType> query,
            FilterSet filter,
            MappingCollection <TEntityType> mappings
            ) where TEntityType : class
        {
            if (!string.IsNullOrWhiteSpace(filter.SortBy))
            {
                var mapping = mappings.GetMapping(filter.SortBy);

                return(mapping.ApplySort(query, filter.SortDir ?? mappings.DefaultSort.SortOrder));
            }

            var defaultSort = mappings.DefaultSort;

            return(defaultSort.Mapping.ApplySort(query, filter.SortDir ?? mappings.DefaultSort.SortOrder));
        }
Example #3
0
        private IQueryable <TEntityType> ApplySort <TEntityType>(
            IQueryable <TEntityType> query,
            FilterSet filter,
            MappingCollection <TEntityType> mappings
            ) where TEntityType : class
        {
            var isDescending = filter.SortDir == "desc";
            var isAscending  = filter.SortDir == "asc";

            bool?selectedSort = isDescending ? true : (isAscending ? false : (bool?)null);

            if (!string.IsNullOrWhiteSpace(filter.SortBy))
            {
                var mapping = mappings.GetMapping(filter.SortBy);

                return(mapping.ApplySort(query, selectedSort ?? mappings.DefaultSort.Descending));
            }

            var defaultSort = mappings.DefaultSort;

            return(defaultSort.Mapping.ApplySort(query, selectedSort ?? mappings.DefaultSort.Descending));
        }