private static void AddRangeOperationFilter <T>(IFilter filter, string propertyName, string propertyValue, IOperation operation, Connector filterStatementConnector) where T : class
        {
            if (!Equals(operation, Operation.GreaterThan) && !Equals(operation, Operation.LessThan) && !Equals(operation, Operation.GreaterThanOrEqualTo) && !Equals(operation, Operation.LessThanOrEqualTo))
            {
                throw new ArgumentException($"Operation [{operation}] not supported on AddRangeOperationFilter.");
            }

            dynamic filterValue1;
            var     propertyType = propertyName.GetPropertyType <T>();

            if (propertyType == typeof(short) || propertyType == typeof(short?))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Short);
                filter.By <short?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(int) || propertyType == typeof(int?))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Int);
                filter.By <int?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(long) || propertyType == typeof(long?))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Long);
                filter.By <long?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(decimal) || propertyType == typeof(decimal?))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Decimal);
                filter.By <decimal?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
            {
                var date = GetFilterValue(propertyValue, TryParseDelegates.Delegates.DateTime).Date;

                if (Equals(operation, Operation.GreaterThan) || Equals(operation, Operation.LessThanOrEqualTo))
                {
                    filterValue1 = date.AddDays(1).AddMilliseconds(-1);
                }

                else
                {
                    filterValue1 = date;
                }

                filter.By <DateTime?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else
            {
                throw new UnsupportedTypeOnOperationException(propertyType, operation);
            }
        }
示例#2
0
 private void FilterStatementBuilder(IFilter filter, List <SearchCriteria> list)
 {
     foreach (var criteria in list)
     {
         if (criteria.Type == "Decimal")
         {
             foreach (var dec in criteria.FieldValue)
             {
                 Decimal fieldValue = Convert.ToDecimal(dec);
                 filter.By(criteria.FieldName, Operation.ByName(criteria.OperationName), fieldValue, default(Decimal), criteria.Connector);
             }
         }
         else if (criteria.Type == "DateTimeOffset")
         {
             foreach (var date in criteria.FieldValue)
             {
                 DateTimeOffset fieldValue = new DateTimeOffset(DateTime.Parse(date.ToString()));
                 filter.By(criteria.FieldName, Operation.ByName(criteria.OperationName), fieldValue, default(DateTimeOffset?), criteria.Connector);
             }
         }
         else if (criteria.Type == "Int16")
         {
             foreach (var num in criteria.FieldValue)
             {
                 Int16 fieldValue = Convert.ToInt16(num);
                 filter.By(criteria.FieldName, Operation.ByName(criteria.OperationName), fieldValue, default(Int16), criteria.Connector);
             }
         }
         else
         {
             foreach (var str in criteria.FieldValue)
             {
                 filter.By(criteria.FieldName, Operation.ByName(criteria.OperationName), str, null, criteria.Connector);
             }
         }
     }
 }
        private static void AddContainOperationFilter <T>(IFilter filter, string propertyName, string propertyValue, IOperation operation, Connector filterStatementConnector) where T : class
        {
            if (!Equals(operation, Operation.Contains) && !Equals(operation, CustomExpressionOperations.ContainsNoTrim) && !Equals(operation, Operation.DoesNotContain) && !Equals(operation, CustomExpressionOperations.DoesNotContainNoTrim))
            {
                throw new ArgumentException($"Operation [{operation}] not supported on AddContainOperationFilter.");
            }

            var propertyType = propertyName.GetPropertyType <T>();

            if (propertyType == typeof(string))
            {
                filter.By(propertyName, operation, propertyValue, filterStatementConnector);
            }

            else
            {
                throw new UnsupportedTypeOnOperationException(propertyType, operation);
            }
        }
        private static void AddEqualOperationFilter <T>(IFilter filter, string propertyName, string propertyValue, IOperation operation, Connector filterStatementConnector) where T : class
        {
            if (!Equals(operation, Operation.EqualTo) && !Equals(operation, CustomExpressionOperations.EqualToNoTrim) && !Equals(operation, Operation.NotEqualTo) && !Equals(operation, CustomExpressionOperations.NotEqualToNoTrim))
            {
                throw new ArgumentException($"Operation [{operation}] not supported on AddEqualOperationFilter.");
            }

            dynamic filterValue1;

            if (propertyValue == null)
            {
                if (Equals(operation, Operation.EqualTo))
                {
                    operation = Operation.IsNull;
                }

                else if (Equals(operation, CustomExpressionOperations.EqualToNoTrim))
                {
                    operation = CustomExpressionOperations.IsNullNoTrim;
                }

                else if (Equals(operation, CustomExpressionOperations.NotEqualToNoTrim))
                {
                    operation = CustomExpressionOperations.IsNotNullNoTrim;
                }

                else
                {
                    operation = Operation.IsNotNull;
                }
            }

            var propertyType = propertyName.GetPropertyType <T>();

            if (propertyType == typeof(string))
            {
                filterValue1 = propertyValue;

                if (Equals(operation, Operation.IsNull))
                {
                    operation = Operation.IsNullOrWhiteSpace;
                }

                else if (Equals(operation, Operation.IsNotNull))
                {
                    operation = Operation.IsNotNullNorWhiteSpace;
                }

                else if (Equals(operation, CustomExpressionOperations.IsNullNoTrim))
                {
                    operation = CustomExpressionOperations.IsNullOrEmpty;
                }

                else if (Equals(operation, CustomExpressionOperations.IsNotNullNoTrim))
                {
                    operation = CustomExpressionOperations.IsNotNullNorEmpty;
                }

                filter.By <string>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(bool))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Bool);
                filter.By <bool>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(bool?))
            {
                filterValue1 = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.Bool);
                filter.By <bool?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(short))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Short);
                filter.By <short>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(short?))
            {
                filterValue1 = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.Short);
                filter.By <short?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(int))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Int);
                filter.By <int>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(int?))
            {
                filterValue1 = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.Int);
                filter.By <int?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(long))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Long);
                filter.By <long>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(long?))
            {
                filterValue1 = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.Long);
                filter.By <long?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(decimal))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.Decimal);
                filter.By <decimal>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(decimal?))
            {
                filterValue1 = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.Decimal);
                filter.By <decimal?>(propertyName, operation, filterValue1, filterStatementConnector);
            }

            else if (propertyType == typeof(DateTime))
            {
                filterValue1 = GetFilterValue(propertyValue, TryParseDelegates.Delegates.DateTime).Date;

                if (Equals(operation, Operation.EqualTo))
                {
                    var filterValue2 = filterValue1.AddDays(1).AddMilliseconds(-1);
                    filter.By <DateTime>(propertyName, Operation.Between, filterValue1, filterValue2, filterStatementConnector);
                }

                else
                {
                    var filterValue2 = filterValue1.AddDays(1);
                    filter.By <DateTime>(propertyName, Operation.LessThan, filterValue1, filterStatementConnector);
                    filter.By <DateTime>(propertyName, Operation.GreaterThanOrEqualTo, filterValue2, filterStatementConnector);
                }
            }

            else if (propertyType == typeof(DateTime?))
            {
                var nullableDateTime = GetNullableFilterValue(propertyValue, TryParseDelegates.Delegates.DateTime)?.Date;

                if (nullableDateTime != null)
                {
                    filterValue1 = (DateTime)nullableDateTime;

                    if (Equals(operation, Operation.EqualTo))
                    {
                        var filterValue2 = filterValue1.AddDays(1).AddMilliseconds(-1);
                        filter.By <DateTime>(propertyName, Operation.Between, filterValue1, filterValue2, filterStatementConnector);
                    }

                    else
                    {
                        var filterValue2 = filterValue1.AddDays(1);
                        filter.By <DateTime>(propertyName, Operation.LessThan, filterValue1, filterStatementConnector);
                        filter.By <DateTime>(propertyName, Operation.GreaterThanOrEqualTo, filterValue2, filterStatementConnector);
                    }
                }

                else
                {
                    filter.By(propertyName, operation, filterStatementConnector);
                }
            }

            else
            {
                throw new UnsupportedTypeOnOperationException(propertyType, operation);
            }
        }