本文整理汇总了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
//.........这里部分代码省略.........