示例#1
0
        public static IQueryable <T> Filter <T>(this IQueryable <T> query, StoreParams storeParams)
        {
            if (storeParams == null /*|| (storeParams.FilterParams == null || !storeParams.FilterParams.Any() )*/)
            {
                return(query);
            }

            var properties = typeof(T).GetProperties();

            storeParams
            .DetermineFilterOperators(properties)
            .Where(x => properties.Any(y => y.Name.ToLower() == x.PropertyName.ToLower()))
            .ForEach(x =>
            {
                var field = properties.First(y => y.Name.ToLower() == x.PropertyName.ToLower());

                var filterExpression = FilterExpressionBuilder.GetFilterExpression <T>(field, x.Value, x.Operator, x.SubProperties);

                if (filterExpression != null)
                {
                    query = query.Where(filterExpression);
                }
            });

            return(query);
        }
示例#2
0
        /// <summary>
        /// Filters an IQueryable by one or more fields.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="source">The collection to be filtered.</param>
        /// <param name="request">An IFilterRequest with one or more Filter objects by which to filter the collection.</param>
        /// <returns>the IQueryable, filtered to include only items which match the specified filter(s).</returns>
        public static IQueryable <T> Filter <T>(this IQueryable <T> source, IFilterRequest request)
        {
            if (request != null && request.Filters != null && request.Filters.Any())
            {
                var expression = FilterExpressionBuilder.GetFilterExpression <T>(request.Logic, request.Filters);
                return(source.Where(expression));
            }

            return(source);
        }
示例#3
0
        /// <summary>
        /// Filters an IQueryable by one or more fields.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="source">The collection to be filtered.</param>
        /// <param name="filters">One or more Filter objects by which to filter the collection.</param>
        /// <returns>the IQueryable, filtered to include only items which match the specified fitler(s).</returns>
        public static IQueryable <T> Filter <T>(this IQueryable <T> source, IEnumerable <IFilter> filters)
        {
            if (filters != null && filters.Any())
            {
                var expression = FilterExpressionBuilder.GetFilterExpression <T>(FilterLogic.And, filters);
                return(source.Where(expression));
            }

            return(source);
        }