[HowTo] LINQ - First Start # Where Operator

This HowTo introduces how to use the Where operator.

Introduction

The Where operator is used for filtering. For the filtering, a condition is provided. All items matching this condition will pass the filter and will be part of the returned result.

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

Example

IPO Samples Comment
Values: 20,3,17,22,10,25,5 A collection of Numbers
Filter Condition: x > 15 x is the item from values used for the check
Result: 20,17,22,25 the filtered numbers

I - Input, P - Processing, O - Output

Implementation

arrNumbers= {20,3,17,22,10,25,5}
grafik

Method Syntax

arrNumbersFiltered= arrNumbers.Where(Function (x) x > 15).ToArray()

Visuals

grafik
grafik

Query Syntax

(From x In arrNumbers
Where x > 15
Select x).toArray
Visuals

grafik

About the Filter Condition

Generals

As mentioned above it is mandatory that the providing filter condition results with a boolean outcome. Similar what e.g. is done within the UiPath IF Activity inside the Condition block in the same manner the filter condition can be provided to the LINQ.

Working:

  • x < 10 | item is less then 10
  • x Mod 2 = 0 | filter for all even numbers
  • Cdate(x).Date = Now.Date | for all today’s dates

Not working:

  • x.Trim() | is returning a string and not a boolean
  • now.AddDays(1) | is returning a DateTime and not a boolean

Multiple Conditions

Multiple conditions can be formulated by combining single conditions with the available conjunction operators

Examples:

  • x >= 5 AND x<= 10
  • x.Equals(“ABC”) OR x.Contains(“A123”)
  • IsNothing(row(“Name”) OrElse String.IsNullOrEmpty(row(“Name”).toString().Trim())

Other Examples

dtData.AsEnumerable.Where(Function (x) DateTime.TryParseExact(x("Date").toString.Trim, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, nothing)).CopyToDataTable
(From fi In New DirectoryInfo("C:\_RPA\_Demo").GetFiles()
Where Regex.IsMAtch(fi.Name,"\(\d+\)\.docx$", RegexOptions.IgnoreCase)
Select fi.FullName).toArray

Summary

  • with the WHERE operator items can be filtered
  • The provided filter condition has to return a boolean
  • condtions also can be combined by conjunction operators

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