[HowTo] LINQ – First Start # Any Operator

Introduction

The “Any” operator is one of the quantifier operators in LINQ used to check if at least one element of a collection satisfies a given condition or not. If the condition is satisfied for at least one item within the collection, the outcome of the Any operator is true. Otherwise, the outcome from the Any operator is false.

The evaluation condition must be mandatory to return a Boolean outcome. In other words: The result of the condition has to be True or False.

API Details

Example

IPO Samples Comment
Values: {2,5,17,9,13} A collection of numbers
Condition: x < 10 Check if any number is less than 10 in the collection
Result: true the result obtained after applying the filter

I – Input, P – Processing, O – Output

Implementation with essential activities

arrNumbers = {2,5,17,9,13}

A classic way of implementation with essential activities would be

  • ForEach# |item | Values: arrNumbers | TypeArgument = Int32
    • If#: item <10
    • Then assign the boolean result to a hasFound variable.
    • Stop the loop with a break activity

Result: hasFound Variable with True / False Value is indicating if at least one item is matching the condition

Visuals

Implementation with LINQ

Method Syntax

arrNumbersFiltered = arrNumbers.Any(function (x) x < 10)

Every item from the collection is evaluated for the condition x < 10 where x is representing the current checked item from an internal loop.

Visuals

Query Syntax

The Any operator is not available as a building block in the Query Syntax. But it can be combined with an appended Any operator as method syntax. As a result, the strength of both can be utilized.

(From x In arrNumbers 
Select x < 10).Any(Function (b) b)
Visuals

Other Samples

Data Table Example

Check in a data table if there is at least one data row with data column (“ColA”) value = blank

dtVar.AsEnumerable.Any(Function (x) isNothing(x(“ColA”)) OrElse String.IsNullOrEmpty(x(“ColA”).ToString.Trim))

Data Column Example

Check if there is at least one column name which ends with _1

dtVar.Columns.Cast(of DataColumn).Any(Function (x) x.ColumnName.EndsWith(“_1”))

Find Children Example

Check if at least one of the children has the required string

uiChildren.Any(Function (x) x.Get(“innertext”).toString.Trim.Equals(“YourTestString”))

References

Recommendations

  • Training courses from Academy
  • Debugging course from Academy (Highly recommended)

Questions

For questions on your retrieval case open a new topic and get individual support

6 Likes