Пример #1
0
        public static IQueryable <T> Where <T>(this IQueryable <T> source, FilterWithOperators filter)
        {
            if (filter.Filter == null || filter.Filter.Rules.Count == 0)
            {
                return(source);
            }

            String[] conditionArr = new String[filter.Filter.Rules.Count];
            Object[] parametrArr  = GetParameterArray <T>(filter);

            int parameterNumber = 0;
            int conditionIndex  = 0;

            foreach (var rule in filter.Filter.Rules)
            {
                conditionArr[conditionIndex++] = GetConditionString <T>(rule, filter.FilterOperators, ref parameterNumber);
            }

            string fullCondition = String.Join(
                (filter.Filter.GroupOperator == filter.FilterOperators.AndOperator? " and ": " or "),
                conditionArr
                );

            if (parametrArr == null)
            {
                return(source.Where(String.Format("x => {0}", fullCondition)));
            }

            return(source.Where(String.Format("x => {0}", fullCondition), parametrArr));
        }
Пример #2
0
        private static Object[] GetParameterArray <T>(FilterWithOperators filter)
        {
            int countOfRulesWithParameter = filter.Filter.Rules.Count(x => x.Operator != filter.FilterOperators.UserDefinedOperator);

            if (countOfRulesWithParameter == 0)
            {
                return(null);
            }

            Object[] parametrArr = new Object[countOfRulesWithParameter];

            int index = 0;

            foreach (var rule in filter.Filter.Rules)
            {
                Type         entityType   = typeof(T);
                PropertyInfo propertyInfo = entityType.GetProperty(rule.PropertyName);
                Type         propertyType = propertyInfo.PropertyType;

                Object propertyValueObject = TypeDescriptor.GetConverter(propertyType).ConvertFrom(rule.PropertyValue);

                parametrArr[index++] = propertyValueObject;
            }
            return(parametrArr);
        }