The Foreach Loop


The Foreach Loop

The Foreach Loop is the answer to the question "How do I process a collection in the control flow?" The Foreach Loop is based on the Foreach construct in C# or VB.NET. If you understand the concept in those languages, the Foreach Loop concept is pretty simple; for each item in a collection of items, do "something." In the Foreach Loop, the "something" is to execute the set of tasks the Foreach Loop contains.

Foreach Enumerators

The Foreach Enumerator is an object the Foreach Loop uses to retrieve elements from collections. Depending on the type of object you need to enumerate, you use a different Foreach Enumerator. The seven stock Foreach Enumerators covered in the following sections provide the ability to enumerate a wide range of collection types.

Types of Foreach Enumerators

Some Foreach Enumerators provide one value per iteration. For example, the Foreach File Enumerator provides an enumeration of the files in a folder. For each iteration of the Foreach File Enumerator, it generates one value, the filename. There are also Foreach Enumerators that enumerate collections of collections or collections of multiple values called Collection Foreach Enumerators. For example, the Foreach Item Enumerator can provide multiple values for each iteration of the Foreach Loop. Figure 13.2 illustrates how the Foreach Loop uses Foreach Enumerators.

Variable Mappings

To make the Foreach Loop useful, you need to have a way for the tasks in the Foreach Loop to use the values found in the collection the loop enumerates over. To do this, the Foreach Loop supports variable mappings, which are a way to associate a variable with an element.

Figure 13.2 shows how the Foreach Loop, variable mappings, and the Foreach Enumerator work for the collections with one value per element.

Figure 13.2. Foreach Loops use Foreach Enumerators to retrieve elements from collections


The Foreach Enumerator keeps track of which item in the collection is the current element with an internal index that it increments every time the Foreach Loop requests another element. The variable mappings define which variable should receive the current value. In this way, tasks and connection managers have access to the current enumerated value through the variable. The dashed line represents the fact that sometimes tasks use the enumerated value indirectly by referencing a connection that uses the enumerated value.

Figure 13.3 is a little different than Figure 13.2. It represents a situation in which you are using a Foreach Enumerator that enumerates collections. This is a powerful usage pattern used for a number of dynamic, table- or file-driven scenarios. The difference is that for each iteration, the Foreach Enumerator sets the value of multiple variables and is useful when you need to use the same package workflow pattern just like with a single enumerated value but with multiple values. These are called matrix, multi-valued, or Collection enumerators. The Foreach ADO, Foreach NodeList, and Foreach Item Enumerators are or can be Collection enumerators.

Figure 13.3. Tasks use collection element values indirectly through variables


The Foreach Loop Designer

The Foreach Loop designer is a combination of two elements. The base designer with the Collection and Variable Mappings tabs is the dialog box shown in Figure 13.4. The second element is the designer for the Foreach Enumerator. Figure 13.4 shows the Foreach Loop designer with the Collection tab selected and the Foreach Enumerator designer highlighted. Each Foreach Enumerator has its own designer and as you change the enumerator type, the Foreach Enumerator designer also changes.

Figure 13.4. The Foreach Loop designer contains the Foreach Enumerator designer


The Variable Mappings tab shown in Figure 13.5 lets you map the value from the Foreach Enumerator to a variable.

Figure 13.5. The Variable Mappings tab lets you map variables to collection elements


If the Foreach Enumerator only enumerates collections of single elements, the index is always zero. You can add more than one mapping, however. For example, you could have two variables that were both mapped to the same value. This might be useful if one of the variables had an expression that converted the value and one simply provided the value without changing it.

If the Foreach Enumerator is a matrix enumerator, or in other words, each element of the collection is itself a subcollection, the Index field reflects which element of the subcollection you want to map to which variable. For example, if there are three elements in each element collection, you would use an index of 0 (zero) for the first element, 1 for the second, and 2 for the third. Because there is no way for the ForEach Loop designer to know what is contained in each of the elements, there is also no way to specify a more friendly name for a collection element. So, you have to know the order of the elements in the collection.



Microsoft SQL Server 2005 Integration Services
Microsoft SQL Server 2005 Integration Services
ISBN: 0672327813
EAN: 2147483647
Year: 2006
Pages: 200
Authors: Kirk Haselden

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net