Collection Data Types
Code Effects supports properties and fields whose types are arrays or implement IEnumerable, which means arrays, List<T>, IEnumerable<T>, and most other collections. The only exception is dictionaries: a type that implements IDictionary is ignored. The Rule Editor divides collections in two kinds, by the type of their items:
- Collections of value types, which in Code Effects includes strings and enumerations. They are used in rules almost like plain fields.
- Collections of reference types, which a rule queries with a rule of its own, nested inside the main rule.
The examples in this article use the following source object:
using System;
using System.Collections.Generic;
using CodeEffects.Rule.Common.Attributes;
public class Visit
{
[Field(DisplayName = "Visit Date", DateTimeFormat = "MMM dd, yyyy")]
public DateTime Date { get; set; }
[Field(DisplayName = "Reason", Max = 100)]
public string Reason { get; set; }
[Field(DisplayName = "Cost")]
public decimal Cost { get; set; }
}
public class Patient
{
[Field(DisplayName = "Allergies")]
public List<string> Allergies { get; set; } = new List<string>();
[Field(DisplayName = "Readings")]
public List<int> Readings { get; set; } = new List<int>();
[Field(DisplayName = "Visits", CollectionSingleItemDisplayName = "Visit")]
public List<Visit> Visits { get; set; } = new List<Visit>();
}
The fields menu lists the collections of value types, Allergies and Readings, beside the plain fields. The Collections... item at the top of the menu leads to the collections of reference types:

Collections of Value Types
A collection of value types is presented and used like a field of the item's type, with operators that read naturally for a collection. They are contain, do not contain, start with, do not start with, end with, do not end with, count, collection is empty, and collection is not empty:

The value of a condition is entered the same way as the value of a plain field of the item's type: a number for Readings, a string for Allergies, and so on. The ValueInputType property of the field decides whether the author can type it, select it from other fields, or do both.
collection is empty and collection is not empty check two things at once: that the collection is initialized, and that it holds items. A collection that is null is empty.
Use the labels of the operators, which are part of the Help XML, to change the wording or to translate the editor.
Items of Nullable and Special Types
A collection can hold nullable items, including nullable enumerations (List<PatientState?>), and any other supported data type, with one exception: collections of DateTimeOffset are not offered. A collection of durations (List<TimeSpan> decorated with TimeType = TimeType.Duration) is supported, and so is a collection of times of day.
Collections of Unknown Item Type
The editor reads the type of the items from the type of the collection. It cannot do that for non-generic collections such as ArrayList, so it ignores them unless you tell it the type of the items with the NonGenericCollectionUnderlyingType property:
[Field(NonGenericCollectionUnderlyingType = typeof(int))]
public ArrayList Readings { get; set; }
The editor knows StringCollection, BitArray, and BitVector32 without help. The older property CollectionItemType does the same and is deprecated.
Collections of Reference Types
Rule authors work with a collection of reference types with a rule inside of the rule: they choose the collection, and then describe the items they are interested in. The items are the members of the item class, such as the Cost and the Reason of a visit.
Open the Collections... item to see the ways to ask a question about a collection of reference types:

- Exists... reads
exists Visit where ( ... ): at least one item satisfies the inner rule.
- Does not exist... reads
does not exist Visit where ( ... ): no item satisfies it.
- All... reads
all Visits have ( ... ): every item satisfies it.
- First... and Last... read
first Visit has ( ... ) and last Visit has ( ... ): the first or the last item satisfies it.
- Count with... reads
count of Visits with ( ... ), and the author compares the number of items that satisfy the inner rule with a number.
- Count... reads
count of Visits, and the author compares the number of all the items with a number.
After the author picks one of the first six, the editor asks for the collection and opens the parentheses of the inner rule, and the members of the item class become available:

The author continues the way they do in any rule. The inner rule is an ordinary evaluation type rule about the item, with as many conditions as the author needs, and and or clauses, and sections:

A rule can have any number of inner rules, and they can be nested to any depth, if the item class itself has collections of reference types. Inner rules can use reusable rules as well.
Name what the items are with the CollectionSingleItemDisplayName property of the FieldAttribute. The editor says exists Visit where instead of exists Visits where. The default is the display name of the field.
Aggregation
Rule Editor does not do aggregation, such as a sum or an average, on collections for you. Your project may need its own logic for it, so you write it as an in-rule method, which a rule author uses like any other rule element:
using System.Linq;
[Method(DisplayName = "Average Reading")]
public decimal AverageReading() => Readings.Count == 0 ? 0 : (decimal)Readings.Average();
The method returns a number, so a rule can compare it with any other number: Check if Average Reading is greater than 120. A method can also take a collection as a parameter. The simple counting needs no method at all: Count... and Count with... do it.
Collections and Rule Evaluation
The Evaluator evaluates a collection condition against the collection of the object you pass to it. See Rule Evaluation.