Share via


Logical Expression Examples: BizTalk Services

 

Important

Microsoft Azure BizTalk Services (MABS) is being retired, and replaced with Azure Logic Apps. If you currently use MABS, then Move from BizTalk Services to Logic Appsprovides some guidance on moving your integration solutions to Logic Apps.

If you're brand new to Logic Apps, then we suggest getting started here:

Lists Logical Expression Examples in Microsoft Azure BizTalk Services.

Logical Expression Examples

All boolean input values are based on Boolean Literals to represent string values of the CLR System.Boolean type. They are written as True and False.

All string input values are based on String Literals to represent string values of the CLR System.String type. There are two types of string literals:

  • Regular string literals

  • Verbatim string literals

Regular string literals are written as a sequence of characters enclosed in quotation marks (“ “). The regular string literals supported by C# are also supported by the Transform parser except the escape sequence (\x). The escape sequence (\x) is not supported.

Verbatim string literals are written as a sequence of characters enclosed in quotation marks (“ “) and prefixed with the @ symbol. The verbatim string literals supported by C# are also supported by the Transform parser.

The Double.TryParse() method is used to verify that a boolean or string literal value is a valid double value. If not, the argument is converted to a double using the Convert.ToDouble() method.

The String.Compare() method is used to compare string values. Uppercase letters are assumed to occur before lowercase letters. For specific details on how string comparison works, go to String.Compare(String, String, StringComparison).

Logical expression reminders:

  • String literals must use quotations marks.

  • When expressions are being evaluated, Conditional AND (&&) has a higher precedence than Conditional OR (||).

  • Use parenthesis to control the order. For example, using the Conditional OR (||) within parenthesis allows this part of the expression to be evaluated first.

Sample Expressions

Relational Operators: <, >, <=, >=

Requires two inputs of the type double. Operator behavior:

  1. An attempt is made to convert the inputs to double. If both inputs are successfully converted to double, the comparison is made and the appropriate Boolean value is returned. If the conversion to double fails for both inputs, the next check is performed.

  2. A check is done to confirm that both values are of type string. If both values are of type string, an Ordinal StringComparison is performed and the appropriate Boolean value is returned. If both values are not of type string, False is returned.

Sample expressions using the following input values:

Input1

ab123.456

Input2

78.9

Input3

-123.11

Input4

“MSFT”

Input5

“Redmond”

Expression

Result

Input1>=Input3

False

Input1 fails the conversion to double.

Input2<Input3

78.9 < (-123.11) = False

The StringComparison is not performed since the input values are numeric.

Input4<Input5

“MSFT” < “Redmond” = True

The StringComparison is performed since the input values are both string. M occurs before R in the alphabet so the value is True.

Input5<=Input2

False

Input5 fails the conversion to double.

Equality Operators: == and !=

Both operators require two inputs of the type bool. == operator behavior:

  1. An attempt is made to convert the inputs to bool. If both inputs are successfully converted to bool, the comparison is made and the appropriate Boolean value is returned. If the conversion to bool fails for both inputs, the next step is performed.

  2. An attempt is made to convert the inputs to double. If both inputs are successfully converted to double, the comparison is made and the appropriate Boolean value is returned. If the conversion to double fails for both inputs, the next check is performed.

  3. A check is done to confirm that both values are of type string. If both values are of type string, an Ordinal StringComparison is performed and the appropriate Boolean value is returned. If both values are not of type string, False is returned.

The ‘!=’ operator is to return the logical negation of the ‘==’ operator. For example, if a comparison returns True, != returns False.

Sample expressions using the following input values:

Input1

ab123.456

Input2

78.9

Input3

-123.11

Input4

“MSFT”

Input5

“Redmond”

Expression

Result

Input1==Input3

False

Input1 fails the conversion to bool.

Input2==Input3

78.9 == (-123.11) = False

Input2 and Input3 fail the conversion to bool.

Input2 and Input3 succeed the conversion to double. The comparison results in False.

Input4!=Input5

“MSFT” != “Redmond” = True

Input4 and Input5 fail the conversion to bool.

Input4 and Input5 fail the conversion to double.

Input4 and Input5 are of type string so an Ordinal String Comparison is performed.

Input5!=Input2

False

 Input5 fail the conversion to bool.

Logical Negation: !

Requires a single input of the type bool. Operator behavior:

  1. A check is done to determine if the input is of type bool or if the input is of type string that can be converted to bool. If the check succeeds, the logical negation of the input is returned. If the check fails, the next step is performed. Note that the input is not converted to bool.

  2. The input value is converted to double. If the conversion succeeds and the value is zero, True is returned. If the conversion succeeds and the value is non-zero, False is returned.

  3. Lastly, False is returned

Note

After the value is extracted at every step listed above, the negation is already listed.

Sample expressions using the following input values:

Input1

ab123.456

Input2

78.9

Input3

-123.11

Input4

“MSFT”

Input5

“Redmond”

Expression

Result

!Input1

False

Input1 fails the bool value check and fails the conversion to double. So, False is returned.

!Input2

False

Input2 is already a double value and is non-zero. So False is returned.

!Input3

False

Input3 is already a double value and is non-zero. So False is returned.

!Input5

False

Input5 fails the bool value check and fails the conversion to double. So, False is returned.

Conditional Logical Operators: && and ||

Requires two inputs of the type bool. Operator behavior:

  1. A check is done to determine if the inputs are of type bool or if the inputs are of type string that can be converted to bool. If the check succeeds, True is returned. If the check fails, the next step is performed.

  2. The input values are converted to double. If the conversion succeeds and the value is non-zero, True is returned. If the conversion succeeds and the value is zero, False is returned. If the conversion to double fails, the next check is performed.

  3. A check is done to confirm that the values are of type string. If the values are of type string and the string values are not null, True is returned.

  4. Lastly, False is returned.

Sample expressions using the following input values:

Input1

City

Input2

State

Input3

Zip

Logical Expression

City == “Redmond” || State == “WA” && Zip == 98052

Sample Data

Result

City is Redmond, State is WA and Zip is 98052

True

The (State == “WA” && Zip == 98052) sub-expression will return True. The (City == “Redmond”) sub-expression will return True. Therefore, the expression will evaluate to True.

City is Redmond, State is WA and Zip is 12345

True

The (State == “WA” && Zip == 98052) sub-expression will return False. The (City == “Redmond”) sub-expression will return True. Therefore, the expression will evaluate to True.

City is Redmond, State is null and Zip is null

True

The (State == “WA” && Zip == 98052) sub-expression will return False. The (City == “Redmond”) sub-expression will return True. Therefore, the expression will evaluate to True.

City is Seattle, State is WA and Zip is 98052

False

The (State == “WA” && Zip == 98052) sub-expression will return True. The (City == “Redmond”) sub-expression will return False. Therefore, the expression will evaluate to False.

City is Seattle, State is NC and Zip is 98052

False

The (State == “WA” && Zip == 98052) sub-expression will return False. The (City == “Redmond”) sub-expression will return False. Therefore, the expression will evaluate to False.

Error and Data Handling

If an error occurs with a Logical Expression, by default, the entire Transform is aborted. This error handling behavior is configurable. See Error and Data Handling at Expressions in BizTalk Services - Usage and Examples.

See Also

Arithmetic Expression Examples: BizTalk Services
If-Then-Else Expression Example: BizTalk Services
Conditional Assignment Example: BizTalk Services
Expressions in BizTalk Services - Usage and Examples