Skip to main content

Sorting

  • 5 minutes to read

XPO allows you to:

  • sort data items in a data store before retrieving data,
  • sort the already retrieved persistent objects.

Sort Data on the Server Side

Sort Data Using XPQuery<T>

You can retrieve data items via the XPQuery<T> object and use LINQ To XPO to run LINQ queries against the underlying data source.

The following example shows how to get sorted data:

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));
...

Use the OrderBy() and ThenBy() extension methods to sort by multiple properties.

// 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();
}

Sort Data Using XPView, XPCursor, and XPCollection

Before persistent objects are retrieved from a data store, they can be sorted by specific data store fields. To accomplish this, use the properties listed in the following table.

Class

Property

XPView

ViewProperty.Sorting

XPCursor

XPCursor.Sorting

XPCollection

XPBaseCollection.Sorting.

The collection content is sorted on the server side when any of the following filter settings are applied to XPCollection:

The ViewProperty.Sorting property represents a sort order option for a specific data field, while the XPCursor.Sorting and XPBaseCollection.Sorting properties represent a collection of SortProperty objects that provide sorting settings for a data store. A SortProperty object provides the SortProperty.PropertyName and SortProperty.Direction properties that allow the data field and its sort order to be specified.

In the following example, the XPCollection retrieves the top five objects from a data store. Before these objects are retrieved, the data store is sorted against the ‘Name’ field.

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;

The image below shows the result:

Sorting_DataStore

In the following example, the XPView is sorted on the server against the Age column.

// 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)});

The image below shows the result:

Sorting_XPView

Sort Data on the Client Side

After persistent objects have been retrieved from a data store, you can sort them by specific data fields on the client. To accomplish this, use the properties listed in the following table.

Class

Property

XPView

XPView.Sorting

XPDataView

XPDataView.Sorting

XPCollection

XPBaseCollection.Sorting.

XPCollection contents are sorted on the client side, unless the XPBaseCollection.TopReturnedObjects or XPBaseCollection.SkipReturnedObjects filter settings are applied to the collection.

As with the server-side sort settings, the Sorting property represents a collection of SortProperty objects that allows you to specify sort settings for specific view columns and collection fields.

In the following example, the XPView fetches data from a data source and then sorts it by the Type column.

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;

Member Table

Online Knowledge Base