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:
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)); }
}
}
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:
XPCollection<User> users = new XPCollection<User>(session1);
Dim users As New XPCollection(Of User)(session1)
Obtaining users that belong to a role named “Developer”:
XPCollection<User> developers = new XPCollection<User>(session1,
new ContainsOperator(nameof(User.Roles), new BinaryOperator(nameof(Role.RoleName), "Developer")));
Dim developers As New XPCollection(Of User)(session1, _
New ContainsOperator(NameOf(User.Roles), New BinaryOperator(NameOf(Role.RoleName), "Developer")))
Obtaining users that belong to a specific role, using a loaded Role object:
Role managerRole = session1.FindObject<Role>(new BinaryOperator(nameof(Role.RoleName), "Manager"));
XPCollection<User> managers = new XPCollection<User>(session1,
new ContainsOperator(nameof(User.Roles), new BinaryOperator("This", managerRole)));
Dim manager As Role = session1.FindObject(Of Role)(New BinaryOperator(NameOf(Role.RoleName), "Manager"))
Dim managers As New XPCollection(Of User)(session1, _
New ContainsOperator(NameOf(User.Roles), New BinaryOperator("This", managerRole)))
Obtaining users that belong to the “Developer” role and whose name starts with “Smith”:
XPCollection<User> users = new XPCollection<User>(session1,
new GroupOperator(GroupOperatorType.And, new CriteriaOperator[] {
new ContainsOperator(nameof(User.Roles), new BinaryOperator(nameof(Role.RoleName), "Developer")),
new FunctionOperator(FunctionOperatorType.StartsWith, new OperandProperty(nameof(User.Name)), new OperandValue("Smith"))
})
);
Dim users As XPCollection(Of User) = New XPCollection(Of User)(session1, _
New GroupOperator(GroupOperatorType.And, New CriteriaOperator() {
New ContainsOperator(NameOf(User.Roles), New BinaryOperator(NameOf(Role.RoleName), "Developer")),
New FunctionOperator(FunctionOperatorType.StartsWith, New OperandProperty(nameof(User.Name)), New OperandValue("Smith"))
}))
The following code has the same effect as above but uses the CriteriaOperator.Parse method:
XPCollection<User> users = new XPCollection<User>(session1,
CriteriaOperator.Parse("[Roles][[RoleName] = ?] And StartsWith([Name], ?)", "Developer", "Smith"));
Dim users As New XPCollection(Of User)(session1, _
CriteriaOperator.Parse("[Roles][[RoleName] = ?] And StartsWith([Name], ?)", "Developer", "Smith"))
Returning a Boolean value that indicates whether there are any items in the Orders
collection.
CriteriaOperator criteria = new AggregateOperand("Orders", Aggregate.Count) > 0;
// or
CriteriaOperator criteria = new OperandProperty("Orders")[null].Count() > 0;
See Also