[HowTo] LINQ – First Start # All Operator

Introduction

The “All” operator is a quantifier operator in LINQ used to check if each element from the collection satisfies a given condition or not. If the condition is satisfied for each element within the collection, the outcome of the operator is true. Otherwise, the outcome 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

Summary

Example

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

I – Input, P – Processing, O – Output

Implementation with Essential activities

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 indicates if each item is matching the condition

Visuals

Implementation with LINQ

Method Syntax

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

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

Visuals

Query Syntax

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

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

Samples

Data Table Example

Check in a data table if all column (“ColA”) values are not blank / empty

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

Data Column Example

Check if no data column name ends with “_1”

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

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

2 Likes