Back to Devexpress

Sorting

xpo-2037-query-and-shape-data-sorting.md

latest9.6 KB
Original Source

Sorting

  • Aug 24, 2020
  • 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:

csharp
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));
...
vb
Imports System.Linq
Imports DevExpress.Xpo

Dim customers As XPQuery(Of Customer) = Session.DefaultSession.Query(Of Customer)()

' Simple Select with Where and OrderBy clauses
Dim list = From c In customers _
        Where c.Country = "Germany" And c.ContactTitle = "Sales Representative" _
        Order By c.ContactName _
        Select c

For Each customer As Customer In list
    Console.WriteLine(customer.ContactName & Constants.vbTab & _
                  customer.Country & Constants.vbTab & customer.ContactTitle)
Next
...

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

csharp
// 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();
}
vb
' A custom "SortBy" method returns a sorted sequence of Customer objects
Public Function SortByName() As IList(Of Customer)
    Return Session.DefaultSession.Query(Of Customer)() _
        .OrderBy(Function(c) c.Name) _
        .ThenBy(Function(c) c.Age) _
        .ToList()
End Function

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.

csharp
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;
vb
Implements DevExpress.Xpo
Implements DevExpress.Xpo.DB

Class Customer
   Inherits XPObject
   Public Property Name() As String
       Get
           Return fName
       End Get
       Set(ByVal value as String)
           SetPropertyValue(NameOf(Name), fName, value)
       End Set
   End Property
   Private fName As String

   Public Property Age() As Integer
       Get
           Return fAge
       End Get
       Set(ByVal value as Integer)
           SetPropertyValue(NameOf(Age), fAge, value)
       End Set
   End Property
   Private fAge As Integer

End Class
...
' Create an XPCollection that retrieves Customer objects from a data store.
Dim collection As XPCollection = New XPCollection(GetType(Customer), Nothing)

' 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), 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:

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

csharp
// 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)});
vb
' Create an XPView that retrieves Customer objects from a data store.
Dim view As New XPView(Session.DefaultSession, GetType(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:

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.

csharp
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