Exemple #1
0
 /// <summary>
 /// OrderBy
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <param name="orders">排序方式</param>
 /// <returns>SqlPack</returns>
 public override SqlPack OrderBy(NewArrayExpression expression, SqlPack sqlPack, params OrderType[] orders)
 {
     for (var i = 0; i < expression.Expressions.Count; i++)
     {
         SqlBuilderProvider.OrderBy(expression.Expressions[i], sqlPack);
         if (i <= orders.Length - 1)
         {
             sqlPack += $" { (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"))
             {
                 sqlPack += " ASC,";
             }
             else
             {
                 sqlPack += ",";
             }
         }
         else
         {
             sqlPack += " ASC,";
         }
     }
     sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
     return(sqlPack);
 }
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlPack</returns>
        public override SqlPack OrderBy(ListInitExpression expression, SqlPack sqlPack, params OrderType[] orders)
        {
            var array = (expression.ToObject() as IEnumerable <object>)?.ToList();

            if (array != null)
            {
                for (var i = 0; i < array.Count; i++)
                {
                    SqlBuilderProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlPack);
                    if (i <= orders.Length - 1)
                    {
                        sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                    }
                    else if (!array[i].ToString().ToUpper().Contains("ASC") && !array[i].ToString().ToUpper().Contains("DESC"))
                    {
                        sqlPack += " ASC,";
                    }
                    else
                    {
                        sqlPack += ",";
                    }
                }
                sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
            }
            return(sqlPack);
        }
 /// <summary>
 /// OrderBy
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <param name="orders">排序方式</param>
 /// <returns>SqlPack</returns>
 public override SqlPack OrderBy(NewExpression expression, SqlPack sqlPack, params OrderType[] orders)
 {
     for (var i = 0; i < expression.Arguments.Count; i++)
     {
         SqlBuilderProvider.OrderBy(expression.Arguments[i], sqlPack);
         if (i <= orders.Length - 1)
         {
             sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
         }
         else
         {
             sqlPack += " ASC,";
         }
     }
     sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
     return(sqlPack);
 }
Exemple #4
0
 /// <summary>
 /// OrderBy
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <param name="orders">排序方式</param>
 /// <returns>SqlPack</returns>
 public override SqlPack OrderBy(UnaryExpression expression, SqlPack sqlPack, params OrderType[] orders)
 {
     SqlBuilderProvider.OrderBy(expression.Operand, sqlPack, orders);
     return(sqlPack);
 }
Exemple #5
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlPack</returns>
        public override SqlPack OrderBy(MemberExpression expression, SqlPack sqlPack, params OrderType[] orders)
        {
            string tableName = string.Empty;

            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                var type = expression.Expression.Type != expression.Member.DeclaringType ?
                           expression.Expression.Type :
                           expression.Member.DeclaringType;
                tableName = sqlPack.GetTableName(type);
            }
            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                tableName = sqlPack.GetTableName(sqlPack.DefaultType);
            }
            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
                if (orders?.Length > 0)
                {
                    sqlPack += $" { (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++)
                        {
                            SqlBuilderProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlPack);
                            if (i <= orders.Length - 1)
                            {
                                sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!array[i].ToUpper().Contains("ASC") && !array[i].ToUpper().Contains("DESC"))
                            {
                                sqlPack += " ASC,";
                            }
                            else
                            {
                                sqlPack += ",";
                            }
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "List`1" && obj is List <string> list)
                    {
                        for (var i = 0; i < list.Count; i++)
                        {
                            SqlBuilderProvider.OrderBy(Expression.Constant(list[i], list[i].GetType()), sqlPack);
                            if (i <= orders.Length - 1)
                            {
                                sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!list[i].ToUpper().Contains("ASC") && !list[i].ToUpper().Contains("DESC"))
                            {
                                sqlPack += " ASC,";
                            }
                            else
                            {
                                sqlPack += ",";
                            }
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "String" && obj is string str)
                    {
                        SqlBuilderProvider.OrderBy(Expression.Constant(str, str.GetType()), sqlPack);
                        str = str.ToUpper();
                        if (!str.Contains("ASC") && !str.Contains("DESC"))
                        {
                            if (orders.Length >= 1)
                            {
                                sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else
                            {
                                sqlPack += " ASC,";
                            }
                            sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                        }
                    }
                }
            }
            return(sqlPack);
        }
Exemple #6
0
 /// <summary>
 /// OrderBy
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="orders">排序方式</param>
 /// <returns>SqlBuilderCore</returns>
 public SqlBuilderCore <T> OrderBy(Expression <Func <T, object> > expression, params OrderType[] orders)
 {
     this._sqlPack += " ORDER BY ";
     SqlBuilderProvider.OrderBy(expression.Body, this._sqlPack, orders);
     return(this);
 }