Skip to main content

Work with Structures

  • 3 minutes to read

This example shows how to store and retrieve properties that are represented by structures and how to build query criteria based on them.

The Shape.Position property is represented by a Point struct type.

public struct Point {
    [Persistent("Abscissa")]
    public int X;
    public int Y;
}
public class Shape: XPObject {
    public Shape(Session session) : base(session) { }
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName = "";

    [Persistent("Location")]
    public Point Position {
        get { return fPosition; }
        set { SetPropertyValue(nameof(Position), ref fPosition, value); }
    }
    Point fPosition;

}

By default, the property of a struct type (in our case this is the Position property) cannot be stored to the data store unless it is marked with the PersistentAttribute. In this tutorial, the ‘X’ struct member will be stored in the ‘Abscissa’ database column (its name is specified in the PersistentAttribute). The ‘Y’ struct member will be stored in a database column called ‘LocationY’. This is the default name for this struct member. This name is automatically constructed from the name of a struct type property followed by the struct’s member name.

Struct’s members can be accessed as shown below.

session1 = new DevExpress.Xpo.Session();
// ...

Shape shape1 = new Shape(session1);
shape1.Name = "Ellipse";
shape1.Position = new Point() { X = 1, Y = 2 };
shape1.Save();

Struct members can also be used to construct query criteria for the collection. The following code demonstrates how to query the data store for Shapes with Position.X equal to 1.

XPCollection<Shape> xpCollection1 = 
    new XPCollection<Shape>(session1, CriteriaOperator.Parse("Position.X = 1"));

Note

We do not recommend using composite or compound keys for new databases. Composite keys may impose limitations on the default functionality. You may wish to refer to the following help topic to learn more: How to map persistent objects to database tables with composite keys.