Exemple #1
0
        public void GetExpressionTest()
        {
            IQueryable<TestEntity> source = Entities.AsQueryable();

            //空条件
            Expression<Func<TestEntity, bool>> predicate = FilterHelper.GetExpression<TestEntity>();
            Assert.True(source.Where(predicate).SequenceEqual(source));

            //单条件,属性不存在
            FilterRule rule = new FilterRule("Name1", "5", FilterOperate.EndsWith);
            FilterRule rule1 = rule;
            Assert.Throws<OSharpException>(() => FilterHelper.GetExpression<TestEntity>(rule1));

            //单条件
            rule = new FilterRule("Name", "5", FilterOperate.EndsWith);
            predicate = FilterHelper.GetExpression<TestEntity>(rule);
            Assert.True(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.EndsWith("5"))));

            //二级条件
            rule = new FilterRule("Name.Length", 5, FilterOperate.Greater);
            predicate = FilterHelper.GetExpression<TestEntity>(rule);
            Assert.True(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.Length > 5)));

            //多条件,异常
            Assert.Throws<OSharpException>(() => new FilterGroup
            {
                Rules = new List<FilterRule> { rule, new FilterRule("IsDeleted", true) },
                Operate = FilterOperate.Equal
            });

            //多条件
            FilterGroup group = new FilterGroup
            {
                Rules = new List<FilterRule> { rule, new FilterRule("IsDeleted", true) },
                Operate = FilterOperate.And
            };
            predicate = FilterHelper.GetExpression<TestEntity>(group);
            Assert.True(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.Length > 5 && m.IsDeleted)));

            //条件组嵌套
            DateTime dt = DateTime.Now;
            group = new FilterGroup
            {
                Rules = new List<FilterRule>
                {
                    new FilterRule("AddDate", dt, FilterOperate.Greater)
                },
                Groups = new List<FilterGroup> { group },
                Operate = FilterOperate.Or
            };
            predicate = FilterHelper.GetExpression<TestEntity>(group);
            Assert.True(source.Where(predicate).SequenceEqual(source.Where(m => m.AddDate > dt || m.Name.Length > 5 && m.IsDeleted)));
        }
Exemple #2
0
        private static LambdaExpression GetPropertyLambdaExpression(ParameterExpression param, FilterRule rule)
        {
            string[]   propertyNames  = rule.Field.Split('.');
            Expression propertyAccess = param;
            Type       type           = param.Type;

            foreach (string propertyName in propertyNames)
            {
                PropertyInfo property = type.GetProperty(propertyName);
                if (property == null)
                {
                    throw new OSharpException(string.Format(Resources.Filter_RuleFieldInTypeNotFound, rule.Field, type.FullName));
                }
                type           = property.PropertyType;
                propertyAccess = Expression.MakeMemberAccess(propertyAccess, property);
            }
            return(Expression.Lambda(propertyAccess, param));
        }