Ejemplo n.º 1
0
        /// <summary>
        /// 建立 Equals ( 相等 ) 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="value">查询值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Equals <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property, P value)
        {
            Expression right = Expression.Constant(value);
            Type       type  = typeof(P);

            var propertyBody = GetMemberExpression(q, property);

            Expression left = propertyBody;

            //如果是Nullable类型,则把value转化成Nullable类型
            if (type.IsNullableType())
            {
                right = Expression.Convert(right, type);
            }

            //过滤空类型
            if (value.IsNull() || right.IsNull())
            {
                return(q);
            }
            //单独对枚举进行处理,枚举值为0则忽略
            //if (right.IsNullEnum(value)) return q;

            var methodExpr = Expression.Equal(left, right);

            q.AppendExpression(methodExpr);

            return(q);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 建立 In 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="valuse">查询值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> In <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property, params P[] values)
        {
            if (values != null && values.Length > 0)
            {
                Type type = typeof(P);

                object nonNullValues = values;

                //如果是Nullable<X>类型,则转化成X类型
                if (type.IsNullableType())
                {
                    //type = type.GetNonNullableType();

                    //nonNullValues = values.Select(v => Convert.ChangeType(v, type)).ToArray();
                }

                var method = method_Contains.MakeGenericMethod(new Type[] { type });

                var propertyBody = GetMemberExpression(q, property);

                MethodCallExpression methodExpr = Expression.Call(null,
                                                                  method,
                                                                  Expression.Constant(nonNullValues),
                                                                  propertyBody
                                                                  );

                q.AppendExpression(methodExpr);
            }

            return(q);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 建立 Between 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="from">开始值</param>
        /// <param name="to">结束值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Between <T>(this IQueryBuilder <T> q, Expression <Func <T, string> > property, string from, string to)
        {
            if (from.IsNull() || to.IsNull())
            {
                return(q);
            }

            from = from.Trim();
            to   = to.Trim();

            if (from != string.Empty && to != string.Empty)
            {
                var propertyBody  = GetMemberExpression(q, property);
                var constantFrom  = Expression.Constant(from);
                var constantTo    = Expression.Constant(to);
                var constantZero  = Expression.Constant(0);
                var compareMethod = typeof(string).GetMethod("Compare", new Type[] { typeof(string), typeof(string) });

                MethodCallExpression methodExp1 = Expression.Call(null, compareMethod, propertyBody, constantFrom);
                var c1 = Expression.GreaterThanOrEqual(methodExp1, constantZero);
                MethodCallExpression methodExp2 = Expression.Call(null, compareMethod, propertyBody, constantTo);
                var c2 = Expression.LessThanOrEqual(methodExp2, constantZero);
                var c  = Expression.AndAlso(c1, c2);

                q.AppendExpression(c);
            }

            return(q);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 建立 Not ( 非 ) 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Not <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property)
        {
            var propertyBody = GetMemberExpression(q, property);

            var methodExpr = Expression.Not(propertyBody);

            q.AppendExpression(methodExpr);

            return(q);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 建立 Like ( 模糊 ) 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="value">查询值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Like <T>(this IQueryBuilder <T> q, Expression <Func <T, string> > property, string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Trim();

                var propertyBody = GetMemberExpression(q, property);

                MethodCallExpression methodExpr = Expression.Call(propertyBody,
                                                                  typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
                                                                  Expression.Constant(value)
                                                                  );

                q.AppendExpression(methodExpr);
            }

            return(q);
        }
Ejemplo n.º 6
0
        public static IQueryBuilder <T> GreaterThan <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property,
                                                           P value)
        {
            Type type          = typeof(P);
            var  constantvalue = Expression.Constant(value);

            var propertyBody = GetMemberExpression(q, property);

            Expression nonNullProperty = propertyBody;

            //如果是Nullable<X>类型,则转化成X类型
            if (type.IsNullableType())
            {
                type            = type.GetNonNullableType();
                nonNullProperty = Expression.Convert(propertyBody, type);
            }
            var c = Expression.GreaterThan(nonNullProperty, constantvalue);

            q.AppendExpression(c);
            return(q);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 建立 Between 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="from">开始值</param>
        /// <param name="to">结束值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Between <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property, P from, P to)
        {
            Type type         = typeof(P);
            var  constantFrom = Expression.Constant(from);
            var  constantTo   = Expression.Constant(to);

            var propertyBody = GetMemberExpression(q, property);

            Expression nonNullProperty = propertyBody;

            //如果是Nullable<X>类型,则转化成X类型
            if (type.IsNullableType())
            {
                type            = type.GetNonNullableType();
                nonNullProperty = Expression.Convert(propertyBody, type);
            }
            var c1 = Expression.GreaterThanOrEqual(nonNullProperty, constantFrom);
            var c2 = Expression.LessThanOrEqual(nonNullProperty, constantTo);
            var c  = Expression.AndAlso(c1, c2);

            q.AppendExpression(c);

            return(q);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 建立 Equals ( 相等 ) 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="value">查询值</param>
        /// <returns></returns>
        public static IQueryBuilder <T> NotEquals <T, P>(this IQueryBuilder <T> q, Expression <Func <T, P> > property, P value)
        {
            Expression right = Expression.Constant(value);
            Type       type  = typeof(P);

            var propertyBody = GetMemberExpression(q, property);

            Expression left = propertyBody;

            //如果是Nullable类型,则把value转化成Nullable类型
            if (type.IsNullableType())
            {
                right = Expression.Convert(right, type);
            }

            if (!right.IsNull())
            {
                var methodExpr = Expression.NotEqual(left, right);

                q.AppendExpression(methodExpr);
            }

            return(q);
        }