The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

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

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>("Name", ref name, value); }
    }

    [Association("User-Role")]
    public XPCollection<Role> Roles { 
        get { return GetCollection<Role>("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>("RoleName", ref roleName, value); }
    }

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

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);
    
  • Obtaining users that belong to a role named "Developer":

    
    XPCollection<User> developers = new XPCollection<User>(session1, 
        new ContainsOperator("Roles", new BinaryOperator("RoleName", "Developer")));
    
  • Obtaining users that belong to a specific role, using a loaded Role object:

    
    Role managerRole = session1.FindObject<Role>(new BinaryOperator("RoleName", "Manager"));
    XPCollection<User> managers = new XPCollection<User>(session1, 
        new ContainsOperator("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("Roles", new BinaryOperator("RoleName", "Developer")),
            new FunctionOperator(FunctionOperatorType.StartsWith, new OperandProperty("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"));
    
See Also