Back to Devexpress

How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship

xpo-3237-examples-how-to-use-the-containsoperator-for-objects-in-a-many-to-many-relationship.md

latest3.3 KB
Original Source

How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship

  • Oct 09, 2023
  • 3 minutes to read

The ContainsOperator checks if an optionally filtered collection contains at least one object. The following code shows the declaration of the User and Role persistent objects with collection properties connected using a many-to-many relationship:

csharp
public class User : XPObject
{
    public User() : base() { }
    public User(Session session) : base(session) { }

    string name;
    public string Name {
        get { return name; }
        set { SetPropertyValue<string>(nameof(Name), ref name, value); }
    }

    [Association("User-Role")]
    public XPCollection<Role> Roles { 
        get { return GetCollection<Role>(nameof(Roles)); } 
    }
}

public class Role : XPObject
{
    public Role() : base() { }
    public Role(Session session) : base(session) { }

    string roleName;
    public string RoleName {
        get { return roleName; }
        set { SetPropertyValue<string>(nameof(RoleName), ref roleName, value); }
    }

    [Association("User-Role")]
    public XPCollection<User> Users {
        get { return GetCollection<User>(nameof(Users)); }
    }
}
vb
Public Class User
    Inherits XPObject
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            SetPropertyValue(Of String)(NameOf(Name), _name, value)
        End Set
    End Property

    <Association("User-Role")> _
    Public ReadOnly Property Roles() As XPCollection(Of Role)
        Get
            Return GetCollection(Of Role)(NameOf(Roles))
        End Get
    End Property
End Class

Public Class Role
    Inherits XPObject
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub

    Private _roleName As String
    Public Property RoleName() As String
        Get
            Return _roleName
        End Get
        Set(ByVal value As String)
            SetPropertyValue(Of String)(NameOf(RoleName), _roleName, value)
        End Set
    End Property

    <Association("User-Role")> _
    Public ReadOnly Property Users() As XPCollection(Of User)
        Get
            Return GetCollection(Of User)(NameOf(Users))
        End Get
    End Property
End Class

The following code examples demonstrate different ways to get a collection of users, filtered depending on their roles, using the ContainsOperator:

  • Obtaining all users:

  • Obtaining users that belong to a role named “Developer”:

  • Obtaining users that belong to a specific role, using a loaded Role object:

  • Obtaining users that belong to the “Developer” role and whose name starts with “Smith”:

  • Returning a Boolean value that indicates whether there are any items in the Orders collection.

See Also

ContainsOperator