codeeffects

Is it possible to check if collection contains field value?


Given the simplified model below I want to write a rule that says MyCollection contains MyField, but all I get is the list of Data Source items to select as if the ValueInputType for collections only works for User. What should I change in the model to achieve that?

public class MyModel
{
    [Field(Settable = false, DataSourceName = "MyDataSource")]
    public int MyField { get; set; }

    [Field(Settable = false, ValueInputType = ValueInputType.All, DataSourceName = "MyDataSource"))]
    public List<int> MyCollection { get; set; }
}

Solution

  • Per the documentation here, numeric value-typed collections can only use ValueInputType.User (look for one of the "IMPORTANT!" headers in the middle of the article). Therefore, you can't use value of the MyField field in your rule conditions. You need to create an in-rule method to achieve that:

    public bool Contains(List<int> collection, [Parameter(DataSourceName = "MyDataSource")] int i)
    {
        return collection.Contains(i);
    }
    

    Having such an in-rule method, your rule could look like this:

    Check if Contains(MyCollection, OneOfTheMembersOfMyDataSource) is True