.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

How to: Work with Structures

  • 3 min 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.X = 1;
shape1.Position.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] You can use structures to declare primary keys composed of several database columns. For more information, see How to use structures to map persistent objects to database tables with compound or composite multi-column primary keys.