private static Expression <Func <TModel, bool> > ToIntValueExpression <TModel>(FilterItemModel filterItem, bool isNullable)
            where TModel : class
        {
            var type = ReflectionHelper.GetPropertyType <TModel>(filterItem.Column);

            if (type.IsEnum || type.IsNullableEnum())
            {
                if (filterItem.Filter == FilterType.Equal)
                {
                    return(EnumToExpression <TModel>(filterItem, type));
                }

                throw new ArgumentException($"Filter {filterItem.Filter} not supported for type Enum. Only {FilterType.Equal} kan be evaluated.", nameof(filterItem));
            }

            var nullableFilterValue = IntHelper.ToIntOrNull(filterItem.Value);

            if (!nullableFilterValue.HasValue)
            {
                throw new ArgumentException($"{filterItem.Value} is not of type {typeof(int)}.");
            }

            var filterValue = nullableFilterValue.Value;

            if (isNullable)
            {
                var entityParamSelector = ReflectionHelper.MemberSelector <TModel, int?>(filterItem.Column);

                if (filterItem.Filter == FilterType.In)
                {
                    var result = filterItem.CollectionExpression(entityParamSelector, IntHelper.ToIntOrNull);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                var expression = FilterExpressionHelper.GetNullableExpression(filterItem.Filter, filterValue);

                return(entityParamSelector.CombineSelectorParamExpression(expression));
            }
            else
            {
                var entityParamSelector = ReflectionHelper.MemberSelector <TModel, int>(filterItem.Column);

                if (filterItem.Filter == FilterType.In)
                {
                    var result = filterItem.CollectionExpression(entityParamSelector, IntHelper.ToInt);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                var expression = FilterExpressionHelper.GetExpression(filterItem.Filter, filterValue);

                return(entityParamSelector.CombineSelectorParamExpression(expression));
            }
        }
        private static Expression <Func <TModel, bool> > ToStringValueExpression <TModel>(FilterItemModel filterItem)
            where TModel : class
        {
            if (string.IsNullOrWhiteSpace(filterItem.Value.ToString()))
            {
                throw new ArgumentException($"{filterItem.Value} is not of type {typeof(string)}.");
            }

            var filterValue = filterItem.Value.ToString();

            var entityParamSelector = ReflectionHelper.MemberSelector <TModel, string>(filterItem.Column);

            if (filterItem.Filter == FilterType.In)
            {
                var result = filterItem.CollectionExpression(entityParamSelector, x => x.ToString());
                if (result != null)
                {
                    return(result);
                }
            }

            var expression = FilterExpressionHelper.GetExpression(filterItem.Filter, filterValue);

            return(entityParamSelector.CombineSelectorParamExpression(expression));
        }
        private static Expression <Func <TModel, bool> > ToGuidValueExpression <TModel>(FilterItemModel filterItem, bool isNullable)
            where TModel : class
        {
            var nullableFilterValue = GuidHelper.ToGuidOrNull(filterItem.Value);

            if (!nullableFilterValue.HasValue || GuidHelper.GuidIsNullOrEmpty(nullableFilterValue))
            {
                throw new ArgumentException($"{filterItem.Value} is not of type {typeof(Guid)}.");
            }

            var filterValue = nullableFilterValue.Value;

            if (isNullable)
            {
                var entityParamSelector = ReflectionHelper.MemberSelector <TModel, Guid?>(filterItem.Column);

                if (filterItem.Filter == FilterType.In)
                {
                    var result = filterItem.CollectionExpression(entityParamSelector, GuidHelper.ToGuidOrNull);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                var expression = FilterExpressionHelper.GetNullableExpression(filterItem.Filter, filterValue);

                return(entityParamSelector.CombineSelectorParamExpression(expression));
            }
            else
            {
                var entityParamSelector = ReflectionHelper.MemberSelector <TModel, Guid>(filterItem.Column);

                if (filterItem.Filter == FilterType.In)
                {
                    var result = filterItem.CollectionExpression(entityParamSelector, GuidHelper.ToGuid);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                var expression = FilterExpressionHelper.GetExpression(filterItem.Filter, filterValue);

                return(entityParamSelector.CombineSelectorParamExpression(expression));
            }
        }
Esempio n. 4
0
        public void GetCollectionExpressionSingleTest()
        {
            var filter = new FilterItemModel(nameof(DataModel.DataInt), ColumnType.IntColumn, FilterType.In, new List <object> {
                1
            });
            Expression <Func <DataModel, int> > paramSelector = x => x.DataInt;

            int DataTypeFunc(object x) => (int)x;

            var expression = filter.CollectionExpression(paramSelector, DataTypeFunc);

            Assert.Null(expression);
        }
Esempio n. 5
0
        public void GetCollectionExpressionThrowsTest()
        {
            Expression <Func <DataModel, int> > paramSelector = x => x.DataInt;

            int DataTypeFunc(object x) => (int)x;

            var filter = new FilterItemModel(nameof(DataModel.DataInt), ColumnType.IntColumn, new List <object>());

            Assert.Throws <ArgumentException>(() => filter.CollectionExpression(paramSelector, DataTypeFunc));

            filter = new FilterItemModel(nameof(DataModel.DataInt), ColumnType.IntColumn, FilterType.In, 2);
            Assert.Throws <ArgumentException>(() => filter.CollectionExpression(paramSelector, DataTypeFunc));
        }
Esempio n. 6
0
        public void GetCollectionExpressionCollectionTest()
        {
            var filter = new FilterItemModel(nameof(DataModel.DataInt), ColumnType.IntColumn, new List <object> {
                1, 2, 3
            });
            Expression <Func <DataModel, int> > paramSelector = x => x.DataInt;

            int DataTypeFunc(object x) => (int)x;

            var expression = filter.CollectionExpression(paramSelector, DataTypeFunc);

            Assert.NotNull(expression);

            var models = new List <DataModel> {
                Model
            };

            Assert.Single(models.Where(expression.Compile()));
        }