當前位置: 首頁>>代碼示例>>C#>>正文


C# SqlQuery.OrderBy方法代碼示例

本文整理匯總了C#中Serenity.Data.SqlQuery.OrderBy方法的典型用法代碼示例。如果您正苦於以下問題:C# SqlQuery.OrderBy方法的具體用法?C# SqlQuery.OrderBy怎麽用?C# SqlQuery.OrderBy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Serenity.Data.SqlQuery的用法示例。


在下文中一共展示了SqlQuery.OrderBy方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ReorderValues

        /// <summary>
        ///   Sets a records display order to to requested value, and also renumbers other records
        ///   in the group as required.</summary>
        /// <param name="connection">
        ///   Connection (required).</param>
        /// <param name="tableName">
        ///   Tablename (required).</param>
        /// <param name="keyField">
        ///   ID field meta that will be used to locate the record (required).</param>
        /// <param name="orderField">
        ///   Display order field meta.</param>
        /// <param name="filter">
        ///   Filter that will determine the record group (can be null).</param>
        /// <param name="recordID">
        ///   ID value of the record.</param>
        /// <param name="newDisplayOrder">
        ///   New display order of the record.</param>
        /// <param name="descendingKeyOrder">
        ///   Will records with same display order values be sorted in ascending or descending ID order?
        ///   For example, if records with ID's 1, 2, 3 has display order value of "0", their actual display
        ///   orders are 1, 2 and 3. If this parameter is set to true (descending), their display orders will
        ///   become 3, 2, 1. This parameter controls if records that are added recently and has no display
        ///   order value assigned (or 0) be shown at start or at the end.</param>
        /// <returns>
        ///   If any of the display order values is changed true.</returns>
        public static bool ReorderValues(IDbConnection connection, string tableName, Field keyField, Field orderField,
            ICriteria filter = null, Int64? recordID = null, int newDisplayOrder = 1,
            bool descendingKeyOrder = false, bool hasUniqueConstraint = false)
        {
            if (connection == null)
                throw new ArgumentNullException("connection");
            if (tableName == null || tableName.Length == 0)
                throw new ArgumentNullException("tableName");
            if (ReferenceEquals(null, keyField))
                throw new ArgumentNullException("keyField");
            if (ReferenceEquals(null, orderField))
                throw new ArgumentNullException("orderField");

            // last assigned display order value
            int order = 0;

            // a list that will contain an element for each record, and hold old and new display
            // order values of records
            List<OrderRecord> orderRecords = new List<OrderRecord>();

            // link to the order entry for record whose display order value is asked to be changed
            OrderRecord changing = null;

            // query to fetch id and display order values of the records in the group
            SqlQuery query = new SqlQuery()
                .Select(
                    keyField,
                    orderField)
                .From(
                    tableName, Alias.T0)
                .Where(
                    filter)
                .OrderBy(
                    orderField);

            // determine display order for records with same display order values
            // based on ID ordering set
            query.OrderBy(keyField.Name, desc : descendingKeyOrder);

            // read all existing records
            using (IDataReader reader = SqlHelper.ExecuteReader(connection, query))
            {
                while (reader.Read())
                {
                    // each records actual display order value is one more than previous one
                    order++;
                    // create an entry to hold current and new display order value of the record
                    OrderRecord r = new OrderRecord();
                    // record ID
                    r.recordID = Convert.ToInt64(reader.GetValue(0));
                    // old display order field value (not the actual display order!)
                    r.oldOrder = Convert.ToInt32(reader.GetValue(1));
                    // new display order value (actual one to be set)
                    r.newOrder = order;

                    orderRecords.Add(r);

                    // if this is the one that is requested to be changed, hold a link to its entry
                    if (recordID == r.recordID)
                        changing = r;
                }
            }

            // ensure that the new display order is within limits
            // if its lower than 1 or bigger than record count, fix it
            if (newDisplayOrder <= 0)
                newDisplayOrder = 1;
            else if (newDisplayOrder > order)
                newDisplayOrder = order;

            // if the record whose display order is to be changed can be found, and its display order value is different
            // than the one in database
            if (changing != null && changing.newOrder != newDisplayOrder)
            {
                // let's say record had a display order value of 6and now it will become 10, the records with actual
//.........這裏部分代碼省略.........
開發者ID:CodeFork,項目名稱:Serenity,代碼行數:101,代碼來源:DisplayOrderHelper.cs


注:本文中的Serenity.Data.SqlQuery.OrderBy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。