Пример #1
0
        public void AddFilterProperty(string propertyName, object propertyValue, string extensionMethod,
                                      OperatorEnumeration operatorAction, bool isNegation = false)
        {
            var property     = _context.DataSetType.GetProperty(propertyName);
            var propertyType = property?.PropertyType;

            if (propertyType != typeof(string) && extensionMethod != LinqOperatorConstants.ConstantEquals)
            {
                throw new ArgumentException($"Property ${propertyName} not support method ${extensionMethod}");
            }

            if (propertyType == null)
            {
                return;
            }

            var propertyAccess =
                Expression.MakeMemberAccess(_context.ParameterExpression,
                                            property ?? throw new InvalidOperationException());
            var methodInfo       = propertyType.GetMethod(extensionMethod, new[] { propertyType });
            var castValue        = ObjectCasterUtil.CastPropertiesType(property, propertyValue);
            var propertyConstant = Expression.Constant(castValue, propertyType);

            if (propertyType.IsGenericType &&
                propertyType.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                propertyType == typeof(DateTime?))
            {
                var converted = Expression.Convert(propertyConstant, typeof(object));
                if (isNegation)
                {
                    AddNegationExpression(operatorAction, propertyAccess, methodInfo, converted);
                }
                else
                {
                    AddNormalExpression(operatorAction, propertyAccess, methodInfo, converted);
                }
            }
            else
            {
                if (isNegation)
                {
                    AddNegationExpression(operatorAction, propertyAccess, methodInfo, propertyConstant);
                }
                else
                {
                    AddNormalExpression(operatorAction, propertyAccess, methodInfo, propertyConstant);
                }
            }
        }
Пример #2
0
        public void AddFilterListProperty(string propertyName, object propertyValue, OperatorEnumeration operatorAction)
        {
            var property     = _context.DataSetType.GetProperty(propertyName);
            var propertyType = property?.PropertyType;

            if (propertyType == null)
            {
                return;
            }

            var castValue  = ObjectCasterUtil.CastPropertiesTypeList(property, propertyValue);
            var methodInfo = castValue.GetType()
                             .GetMethod(LinqOperatorConstants.ConstantContains, new[] { propertyType });

            var list  = Expression.Constant(castValue);
            var value = Expression.Property(_context.ParameterExpression, propertyName);

            AddNormalExpression(operatorAction, list, methodInfo, value);
        }