排序
- 2020年8月24日
- 5分钟阅读
XPO允许您执行以下操作:
- 在检索数据之前对数据存储中的数据项进行排序,
- 对已经检索到的持久对象进行排序。
#在服务器端对数据排序
#使用XPQuery 排序数据
您可以通过XPQuery 对象检索数据项,并使用LINQ To XPO对基础数据源运行LINQ查询。
以下示例显示如何获取排序的数据:
using System.Linq;
using DevExpress.Xpo;
...
XPQuery<Customer> customers = Session.DefaultSession.Query<Customer>();
// Simple Select with Where and OrderBy clauses
var list = from c in customers
where (c.Country == "Germany" && c.ContactTitle == "Sales Representative")
orderby c.ContactName
select c;
foreach (Customer customer in list)
Console.WriteLine(string.Format("{0}\t{1}\t{2}", customer.ContactName,
customer.Country, customer.ContactTitle));
...
使用OrderBy()
和ThenBy()
扩展方法按多个属性排序。
// A custom "SortBy" method returns a sorted sequence of Customer objects
public IList<Customer> SortByName() {
return Session.DefaultSession.Query<Customer>()
.OrderBy(c => c.Name)
.ThenBy(c => c.Age)
.ToList();
}
#使用XPView,XPCursor和XPCollection对数据进行排序
在从数据存储中检索持久对象之前,可以按特定的数据存储字段对它们进行排序。为此,请使用下表中列出的属性。
类 | 属性 |
---|---|
XPView | 查看属性。排序 |
XP光标 | XPCursor。排序 |
XPCollection | XPBase集合。排序。将以下任何过滤器设置应用于XPCollection时,集合内容将在服务器端排序:XPBase集合。返回最热门的对象XPBase集合。跳过返回的对象。 |
所述ViewProperty.Sorting属性表示对一个特定的数据字段中的排序顺序的选项,而XPCursor.Sorting和XPBaseCollection.Sorting属性表示的集合SortProperty提供用于数据存储排序设置的对象。甲SortProperty对象提供SortProperty.PropertyName和SortProperty.Direction性质允许指定数据字段及其排序顺序。
在下面的示例中,XPCollection从数据存储中检索前五个对象。在检索这些对象之前,将根据“名称”字段对数据存储进行排序。
using DevExpress.Xpo.DB;
using DevExpress.Xpo;
class Customer : XPObject {
public string Name {
get { return fName; }
set { SetPropertyValue(nameof(Name), ref fName, value); }
}
string fName;
public int Age {
get { return fAge; }
set { SetPropertyValue(nameof(Age), ref fAge, value); }
}
int fAge;
}
...
// Create an XPCollection that retrieves Customer objects from a data store.
XPCollection collection = new XPCollection(typeof(Customer), null);
// Populate the Sorting collection. The values in the "Name" field will be sorted in Ascending order
// before the XPCollection retrieves the objects from a data store.
collection.Sorting.Add(new SortProperty(nameof(Customer.Name), DevExpress.Xpo.DB.SortingDirection.Ascending));
// Specify the maximum number of objects retrieved by the collection from a data store.
collection.TopReturnedObjects = 5;
下图显示了结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MOkNwH9o-1612142350782)(https://docs.devexpress.com/XPO/images/sorting_datastore4938.png)]
在以下示例中,XPView在服务器上根据“**年龄”**列进行排序。
// Create an XPView that retrieves Customer objects from a data store.
XPView view = new XPView(Session.DefaultSession, typeof(Customer));
// Populate the view's Properties collection.
view.Properties.AddRange(new ViewProperty[] {
new ViewProperty("Name", SortDirection.None, new OperandProperty(nameof(Customer.Name)), false, true),
new ViewProperty("Age", SortDirection.Descending, new OperandProperty(nameof(Customer.Age)), false, true)});
下图显示了结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTki95xq-1612142350786)(https://docs.devexpress.com/XPO/images/sorting_xpview4939.png)]
#在客户端对数据排序
从数据存储中检索到持久对象后,可以按客户端上的特定数据字段对它们进行排序。为此,请使用下表中列出的属性。
类 | 属性 |
---|---|
XPView | XPView。排序 |
XPData视图 | XPData视图。排序 |
XPCollection | XPBase集合。排序。除非XPBase集合,否则XPCollection的内容在客户端进行排序。顶部返回的对象或XPBase集合。跳过返回的对象筛选器设置将应用于集合。 |
与服务器端排序设置一样,Sorting属性表示SortProperty对象的集合,该对象使您可以为特定视图列和集合字段指定排序设置。
在以下示例中,XPView从数据源中获取数据,然后按“类型”列对其进行排序。
XPView view1 = new XPView(Session.DefaultSession, typeof(Issue), "Name; Type", null);
SortingCollection sortCollection = new SortingCollection();
sortCollection.Add(new SortProperty(nameof(Issue.Type), DevExpress.Xpo.DB.SortingDirection.Descending));
view1.Sorting = sortCollection;
view1.TopReturnedRecords = 2;
SortProperty(nameof(Issue.Type), DevExpress.Xpo.DB.SortingDirection.Descending));
view1.Sorting = sortCollection;
view1.TopReturnedRecords = 2;