Looping Through a Collection

  • Updated

Prerequisites: A valid Entity
Namespaces: InRule.Runtime
Classes: Field, Collection, Entity
See Also: Retrieving a Rule Application, Creating a RuleSession, Creating Entities

Entity based collections

// loop over all the payments collection members
foreach (EntityCollectionMember payment in mortgageEntity.Collections["Payments"])
{
 // Write out some of the entity field values
 Console.WriteLine(payment.Fields["PaymentDate"].Value.ToString());
 Console.WriteLine(payment.Fields["Amount"].Value.ToString());
 Console.WriteLine(payment.Fields["RemainingBalance"].Value.ToString());
}

An alternative way to loop through the entities themselves is as follows:

// Check if the collection of entities
if (collection is InRule.Runtime.EntityCollection)
{
    // process a collection of entities
    foreach (EntityCollectionMember member in collection)
    {
        // Get the entity the member refers to
        Entity memberEntity = member.Value.ToEntity();
        // Iterate the fields
        foreach (Field field in memberEntity.Fields)
        {
            // get the fields info
            Console.WriteLine("FieldName:" + field.Name);
            Console.WriteLine("FieldValue:" + field.Value.ToString());
        }
    }
}

Complex collections
// Loop through the members in the complex collection AdditionalCharges
foreach (CollectionMember charge in mortgageEntity.Collections["AdditionalCharges"])
{
  // write the field values
  Console.WriteLine(charge.Fields["Name"].Value.ToString());
  Console.WriteLine(charge.Fields["Amount"].Value.ToString());
}

Note: Collection indexing is 0-based through the SDK and 1-based for referencing within rule

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.