Esempio n. 1
0
        private IQueryable <T> SmartWhereBinaryExpression(Expression <Func <T, bool> > predicate)
        {
            // parse the body
            BinaryExpression ex = (BinaryExpression)predicate.Body;

            if (ex.NodeType != ExpressionType.Equal)
            {
                return(null);
            }

            // get left value
            MemberExpression left = (MemberExpression)ex.Left;

            if (left.Expression.NodeType != ExpressionType.Parameter)
            {
                return(null);
            }
            string leftName = left.Member.Name;

            // check right value
            if (ex.Right.NodeType == ExpressionType.MemberAccess && ((MemberExpression)ex.Right).Expression.NodeType == ExpressionType.Parameter)
            {
                return(null);
            }
            object value = Expression.Lambda(ex.Right).Compile().DynamicInvoke();

            if (value is Boolean)
            {
                value = (bool)value ? 1 : 0;
            }
            if (!(value is Int32) && !(value is String))
            {
                return(null);
            }

            // check if mapping supports SQL compare
            var mappingList = mapping.Where(x => x.PropertyName == leftName);

            if (mappingList.Count() == 0)
            {
                return(null);
            }
            SQLFieldMapping thisMapping = mappingList.First();

            if (!Attribute.IsDefined(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute)))
            {
                return(null);
            }

            // build SQL
            AllowSQLCompareAttribute attr = (AllowSQLCompareAttribute)Attribute.GetCustomAttribute(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute));

            // create smart where
            whereItems.Add(new Tuple <string, object>(attr.GetSQLCondition(thisMapping), value));
            return(this);
        }
Esempio n. 2
0
        private IQueryable <T> SmartWhereMethodCall(Expression <Func <T, bool> > predicate)
        {
            MethodCallExpression mce = predicate.Body as MethodCallExpression;

            // verify some preconditions
            if (mce.Method.Name != "Contains")
            {
                return(null);
            }
            if (mce.Arguments.Count != 1 || mce.Arguments[0].NodeType != ExpressionType.MemberAccess)
            {
                return(null);
            }
            if (mce.Object.NodeType != ExpressionType.MemberAccess)
            {
                return(null);
            }

            // get value
            object value = Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke();

            if (!(value is Int32) && !(value is String))
            {
                return(null);
            }

            // check mapping
            string field       = (mce.Object as MemberExpression).Member.Name;
            var    mappingList = mapping.Where(x => x.PropertyName == field);

            if (mappingList.Count() == 0)
            {
                return(null);
            }
            SQLFieldMapping thisMapping = mappingList.First();

            if (!Attribute.IsDefined(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute)))
            {
                return(null);
            }

            // build SQL
            AllowSQLCompareAttribute attr = (AllowSQLCompareAttribute)Attribute.GetCustomAttribute(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute));

            whereItems.Add(new Tuple <string, object>(attr.GetSQLCondition(thisMapping), value));
            return(this);
        }