Esempio n. 1
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlWrapper">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlWrapper</returns>
        public override SqlWrapper OrderBy(ListInitExpression expression, SqlWrapper sqlWrapper, params OrderType[] orders)
        {
            var array = (expression.ToObject() as IEnumerable <object>)?.ToList();

            if (array != null)
            {
                for (var i = 0; i < array.Count; i++)
                {
                    SqlExpressionProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlWrapper);

                    if (i <= orders.Length - 1)
                    {
                        sqlWrapper += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                    }
                    else if (!array[i].ToString().ToUpper().Contains("ASC") && !array[i].ToString().ToUpper().Contains("DESC"))
                    {
                        sqlWrapper += " ASC,";
                    }
                    else
                    {
                        sqlWrapper += ",";
                    }
                }
                sqlWrapper.Remove(sqlWrapper.Length - 1, 1);
            }

            return(sqlWrapper);
        }
Esempio n. 2
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlWrapper">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlWrapper</returns>
        public override SqlWrapper OrderBy(NewArrayExpression expression, SqlWrapper sqlWrapper, params OrderType[] orders)
        {
            for (var i = 0; i < expression.Expressions.Count; i++)
            {
                SqlExpressionProvider.OrderBy(expression.Expressions[i], sqlWrapper);

                if (i <= orders.Length - 1)
                {
                    sqlWrapper += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                }
                else if (expression.Expressions[i] is ConstantExpression order)
                {
                    if (!order.Value.ToString().ToUpper().Contains("ASC") && !order.Value.ToString().ToUpper().Contains("DESC"))
                    {
                        sqlWrapper += " ASC,";
                    }
                    else
                    {
                        sqlWrapper += ",";
                    }
                }
                else
                {
                    sqlWrapper += " ASC,";
                }
            }

            sqlWrapper.Remove(sqlWrapper.Length - 1, 1);

            return(sqlWrapper);
        }
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlWrapper">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlWrapper</returns>
        public override SqlWrapper OrderBy(NewExpression expression, SqlWrapper sqlWrapper, params OrderType[] orders)
        {
            for (var i = 0; i < expression.Arguments.Count; i++)
            {
                SqlExpressionProvider.OrderBy(expression.Arguments[i], sqlWrapper);

                if (i <= orders.Length - 1)
                {
                    sqlWrapper += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                }
                else
                {
                    sqlWrapper += " ASC,";
                }
            }

            sqlWrapper.Remove(sqlWrapper.Length - 1, 1);

            return(sqlWrapper);
        }
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlWrapper">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlWrapper</returns>
        public override SqlWrapper OrderBy(MemberExpression expression, SqlWrapper sqlWrapper, params OrderType[] orders)
        {
            var tableAlias = string.Empty;
            var tableName  = string.Empty;

            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                var type = expression.Expression.Type != expression.Member.DeclaringType ?
                           expression.Expression.Type :
                           expression.Member.DeclaringType;
                tableName = sqlWrapper.GetTableName(type);
            }

            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                tableName = sqlWrapper.GetTableName(sqlWrapper.DefaultType);
            }

            tableAlias = sqlWrapper.GetTableAlias(tableName, tableAlias);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }

            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                sqlWrapper += tableAlias + sqlWrapper.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
                if (orders?.Length > 0)
                {
                    sqlWrapper += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")}";
                }
            }

            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                var obj = expression.ToObject();
                if (obj != null)
                {
                    var type = obj.GetType().Name;

                    if (type == "String[]" && obj is string[] array)
                    {
                        for (var i = 0; i < array.Length; i++)
                        {
                            SqlExpressionProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlWrapper);

                            if (i <= orders.Length - 1)
                            {
                                sqlWrapper += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!array[i].ToUpper().Contains("ASC") && !array[i].ToUpper().Contains("DESC"))
                            {
                                sqlWrapper += " ASC,";
                            }
                            else
                            {
                                sqlWrapper += ",";
                            }
                        }
                        sqlWrapper.Remove(sqlWrapper.Length - 1, 1);
                    }

                    if (type == "List`1" && obj is List <string> list)
                    {
                        for (var i = 0; i < list.Count; i++)
                        {
                            SqlExpressionProvider.OrderBy(Expression.Constant(list[i], list[i].GetType()), sqlWrapper);

                            if (i <= orders.Length - 1)
                            {
                                sqlWrapper += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!list[i].ToUpper().Contains("ASC") && !list[i].ToUpper().Contains("DESC"))
                            {
                                sqlWrapper += " ASC,";
                            }
                            else
                            {
                                sqlWrapper += ",";
                            }
                        }
                        sqlWrapper.Remove(sqlWrapper.Length - 1, 1);
                    }

                    if (type == "String" && obj is string str)
                    {
                        SqlExpressionProvider.OrderBy(Expression.Constant(str, str.GetType()), sqlWrapper);
                        str = str.ToUpper();
                        if (!str.Contains("ASC") && !str.Contains("DESC"))
                        {
                            if (orders.Length >= 1)
                            {
                                sqlWrapper += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else
                            {
                                sqlWrapper += " ASC,";
                            }

                            sqlWrapper.Remove(sqlWrapper.Length - 1, 1);
                        }
                    }
                }
            }

            return(sqlWrapper);
        }
Esempio n. 5
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlWrapper">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlWrapper</returns>
        public override SqlWrapper OrderBy(UnaryExpression expression, SqlWrapper sqlWrapper, params OrderType[] orders)
        {
            SqlExpressionProvider.OrderBy(expression.Operand, sqlWrapper, orders);

            return(sqlWrapper);
        }