This HowTo introduces the Contains operator
Contains Operator
The Contains Operator from LINQ will check if a particular item is present within a set of items and will return true (when present) or false (when not present).
Overview
I – Input | P – Processing | O – Output |
---|---|---|
{"A1", "B1", "C1" } |
check if “B1” is contained at least 1 time | True |
{"AA", "BB", "CC" } |
check if “B” is contained at least 1 time | False |
{1,2,3,4 } |
check if 5 is contained at least 1 time | False |
Constraints
For the comparison:
- an existing default equality comparer is used
- The data type of the set elements must be compatible with it
Alternate option:
- Provide a custom equality comparer for the comparison
Implementation
Method Syntax
arrValues | String( ) - a String Array = new String(){"A1", "B1", "C1" }
hasFound | Boolean = arrValues.Contains("B1") | return: True
Visuals
arrValues | String( ) - a String Array = new String(){"AA", "BB", "CC" }
hasFound | Boolean = arrValues.Contains("B") | return: False
The Contains operator is not to be mixed up with the Contains method of the String class.
The Contains operator checks for the presence of the item in the item set.
Visuals
Query Syntax
The Contains operator is not available as a clause for the query syntax
About equality comparers
An equality comparer implements how two objects (items) are compared for equality.
As shown above, the default equality comparer works successfully and does not need to be explicitly specified. However, for certain data types, it is necessary to pass a specific equality comparer.
Read more about comparers here: [HowTo] LINQ - About # Comparers
Strings / StringComparer
An Equality Comparer specialized for strings is available and can be used e.g. for the case insensitive check.
Datarows / DataRowComparer
If a data table is to be determined to contain a certain data row, this will fail if the default equality comparer is used.
The second row of dt1 and the first row of dt2 can be evaluated as equal because the values of the data row columns are equal: BB,2 = BB,2
Without the use of a specialized equality comparer for data rows, a contains evaluates to false.
For Datarow equality comparisons, the DataRowComparer can be used and offers a default comparison logic.
With the use of the DataRowComparer, a contains evaluates to false
Array / Failing Equality Comparison
In the following, it is demonstrated that the default equality comparer fails e.g. with arrays.
With the out-of-the-box equality comparer, the arrays are not compared on their value basis. Therefore the Contains evaluates to False.
In the .Net world, this case would be addressed with a custom Equality comparer.
In the UiPath world, possible workarounds would be
-
List(Of Tuple(Of String, String) as an alternative datatype
-
A LINQ statement with the Any operator
References
Documentations:
- Enumerable.Contains Methode (System.Linq) | Microsoft Learn
- IEqualityComparer<T> Interface (System.Collections.Generic) | Microsoft Learn
- StringComparer Class (System) | Microsoft Learn
- DataRowComparer<TRow> Class (System.Data) | Microsoft Learn
Questions
For questions on your use case open a new topic and get individual support