Exemplo n.º 1
0
        /// <summary>
        /// create in expression ( operator "=in=" )
        /// </summary>
        /// <returns></returns>
        public static Expression <Func <T, bool> > GetInExpression <T>(ParameterExpression parameter,
                                                                       QueryParser.ComparisonContext context,
                                                                       NamingStrategy namingStrategy = null)
        {
            if (parameter == null)
            {
                throw new ArgumentException(nameof(parameter));
            }
            if (context == null)
            {
                throw new ArgumentException(nameof(context));
            }
            var expressionValue = ExpressionValue.Parse <T>(parameter, context.selector().GetText(), namingStrategy);

            if (!EqOrNeqOrInOrOutAutorizedType.Contains(expressionValue.Property.PropertyType))
            {
                throw new QueryComparisonInvalidComparatorSelectionException(context);
            }
            var values = QueryGetValueHelper.GetValues(expressionValue.Property.PropertyType, context.arguments());

            if (values == null || values.Count == 0)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }

            var methodContainsInfo =
                QueryReflectionHelper.GetOrRegistryContainsMethodInfo(expressionValue.Property.PropertyType);

            return(Expression.Lambda <Func <T, bool> >(
                       Expression.Call(Expression.Constant(methodContainsInfo.Convert(values)),
                                       methodContainsInfo.ContainsMethod,
                                       expressionValue.Expression), parameter));
        }
Exemplo n.º 2
0
        public static ExpressionValue Parse <T>(ParameterExpression parameter,
                                                string selector,
                                                NamingStrategy namingStrategy)
        {
            if (parameter == null)
            {
                throw new ArgumentException(nameof(parameter));
            }
            if (selector == null)
            {
                throw new ArgumentException(nameof(selector));
            }
            Expression   lastMember = parameter;
            PropertyInfo property   = null;
            var          type       = typeof(T);

            if (selector.IndexOf(".", StringComparison.InvariantCulture) != -1)
            {
                foreach (var item in selector.Split('.'))
                {
                    property = QueryReflectionHelper.GetOrRegistryProperty(type, item, namingStrategy);
                    if (property == null)
                    {
                        throw new Exception($"Invalid property {selector}");
                    }
                    type       = property.PropertyType;
                    lastMember = Expression.Property(lastMember, property);
                }
            }
            else
            {
                property = QueryReflectionHelper.GetOrRegistryProperty(type, selector, namingStrategy);
                if (property == null)
                {
                    throw new Exception($"Invalid property {selector}");
                }
                lastMember = Expression.Property(lastMember, property);
            }

            return(new ExpressionValue()
            {
                Property = property,
                Expression = lastMember
            });
        }