Loop statement (foreach)
BI# is built for data-heavy applications and its foreach
statement allows iteration over large sets of data. This example of
a function uses the foreach
statement to loop over the
result set of an SQL query:
void Process(SQLConnection connection)
{
SQLData resultData = SQLExecuteQuery(
connection,
"Select * from employees"
);
foreach (SQLDataRow row in resultData)
{
string name = SQLDataRowGetString(row,"name");
WriteLine("Name: " + name);
}
}
When iterating over lists of data you often must filter elements. You
can use
if
statements for this purpose, but
foreach
supports a where clause that
solves this problem more elegantly:
void Process(OLAPConnection con)
{
OLAPElementList list = OLAPGetElementList(con,"DACOUNT", true);
foreach (OLAPElement account in list
where OLAPGetStringAttribute(account, "ALRE") == "A")
{
// Do something with account.
}
}
The
foreach
statement has this structure:
foreach (<type> <name> in <list expression>
[where <condition>]) <block>
The statement creates a local variable named
name
of the specified type. For each
iteration, BI# fills this variable with the next item from the
list
expression. For every list type,
BI# allows a certain set of iterator variable types. This table lists possible
combinations of iterator variable types and list types:
List Type | Iterator Variable Type |
---|---|
OLAPElementList
|
OLAPElement
|
OLAPDataArea
|
OLAPCell
|
SQLData
|
SQLDataRow
|
StringArray
|
String
|
OLAPElementNativeArray
|
OLAPElementNative
|
OLAPElementXMLAArray
|
OLAPElementXMLA
|
OLAPAttributeTable
|
OLAPAttribute
|
OLAPCubeList
|
OLAPCube
|
OLAPDimensionList
|
OLAPDimension
|
OLAPAttributeTableList
|
OLAPAttributeTable
|
If you do not specify a
where
clause or if the
where
clause's condition is true, BI#
runs the loop's block. Otherwise, it starts the next iteration.