Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="expression"></param>
        /// <param name="isNot"></param>
        /// <param name="isEqualsTo"></param>
        /// <returns></returns>
        private static QueryGroup ParseContainsForArrayOrList <TEntity>(MethodCallExpression expression,
                                                                        bool isNot,
                                                                        bool isEqualsTo)
            where TEntity : class
        {
            // TODO: Refactor this

            // Return null if there is no any arguments
            if (expression.Arguments?.Any() != true)
            {
                return(null);
            }

            // Get the last arg
            var last = expression
                       .Arguments
                       .LastOrDefault();

            // Make sure the last arg is a member
            if (last == null || last?.IsMember() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported.");
            }

            // Make sure it is a property info
            var member = last.ToMember().Member;

            if (member.IsPropertyInfo() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported.");
            }

            // Get the property
            var property = member.ToPropertyInfo();

            // Make sure the property is in the entity
            if (PropertyCache.Get <TEntity>().FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, property.Name, StringComparison.OrdinalIgnoreCase)) == null)
            {
                throw new InvalidExpressionException($"Invalid expression '{expression.ToString()}'. The property {property.Name} is not defined on a target type '{typeof(TEntity).FullName}'.");
            }

            // Get the values
            var values = (object)null;

            // Array/List Separation
            if (expression.Object == null)
            {
                // Expecting an array
                values = expression.Arguments.First().GetValue();
            }
            else
            {
                // Expecting a list here
                values = expression.Object.GetValue();
            }

            // Add to query fields
            var operation  = (isNot == false && isEqualsTo == true) ? Operation.In : Operation.NotIn;
            var queryField = new QueryField(PropertyMappedNameCache.Get(property), operation, values);

            // Return the result
            var queryGroup = new QueryGroup(queryField);

            // Set the IsNot value
            queryGroup.SetIsNot(isNot == true && isEqualsTo == false);

            // Return the instance
            return(queryGroup);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="expression"></param>
        /// <param name="rightValue"></param>
        /// <param name="expressionType"></param>
        /// <param name="isNot"></param>
        /// <param name="isEqualsTo"></param>
        /// <returns></returns>
        private static QueryGroup Parse <TEntity>(MemberExpression expression,
                                                  object rightValue,
                                                  ExpressionType expressionType,
                                                  bool isNot,
                                                  bool isEqualsTo)
            where TEntity : class
        {
            var queryGroup   = (QueryGroup)null;
            var value        = rightValue;
            var isForBoolean = expression.Type == typeof(bool) &&
                               (expressionType == ExpressionType.Not || expressionType == ExpressionType.AndAlso || expressionType == ExpressionType.OrElse);
            var ignoreIsNot = false;

            // Handle for boolean
            if (value == null)
            {
                if (isForBoolean)
                {
                    value       = false;
                    ignoreIsNot = true;
                }
                else
                {
                    value = expression.GetValue();
                }
            }

            // Check if there are values
            if (value != null)
            {
                // Specialized for enum
                if (expression.Type.IsEnum)
                {
                    value = Enum.ToObject(expression.Type, value);
                }

                // Create a new field
                var field = (QueryField)null;

                if (isForBoolean)
                {
                    field = new QueryField(expression.Member.GetMappedName(),
                                           value);
                    ignoreIsNot = true;
                }
                else
                {
                    field = new QueryField(expression.Member.GetMappedName(),
                                           QueryField.GetOperation(expressionType),
                                           value);
                }

                // Set the query group
                queryGroup = new QueryGroup(field);

                // Set the query group IsNot property
                if (ignoreIsNot == false)
                {
                    queryGroup.SetIsNot(isEqualsTo == false);
                }
            }

            // Return the result
            return(queryGroup);
        }