How to use LINQ in UiPath

Linq stands for Language Integrated Query that enables you to retreive data.Linq is commonly used to query arrays, data tables, dictionaries, lists and any data type that is collection.

Both vb.net & C# supports LINQ

Syntax will be differerent for both.

How to use LINQ in UiPath

To use LINQ in UiPath, you have to import System.LINQ namespace.

What is Lamba Expression in LINQ

Lamda expression is used to call a function without name. In lamba function we can pass a parameter and return a value.

→Syntax of Lamda Expression in vb.net

function(parameter)parameter* parameter

→Syntax of Lamda Expression in C#

parameter =>parameter* parameter

Types of Syntax used in LINQ

There are 2 types of Syntax used in LINQ-

  1. Method Syntax — Method syntax uses predefined methods to query data from a data source. Example- arrValues (Array of Integer)

arrValues.Select(function(item)item item).ToArray*

  1. **Query Syntax-**Query syntax uses query like expression to retrieve data from a data source. Example- arrValues (Array of Integer)

(From item in arrValues select item*item).ToArray

Most Commonly used Methods in LINQ

There are some most commonly used methods in LINQ. For Example-

Select, Where , Any,All,GroupBy,OrderBy, Join,Aggregate function(Sum,Max,Min),Take,TakeWhile,Skip,SkipWhile,First,FirstOrDefault,Last,LastOrDefault,Except,Intersect,Distinct.

  • Implementation in UiPath*

Example 1- Print the square of numbers.

Step1:- Create a variable arrValues of type Array of Integer i.e int32

Step 2:- We will now use For each loop to loop through each item of an array.

As we have to print the square of the numbers, so we have use Method syntax of LINQ to fetch the result.

Result:-
image

Example 2- Print the sum of even square numbers.

arrValues.where(function(d) d mod 2=0).Select(function(d) d*d)

Result:-
image

Now, we need to sum the square of even numbers, so will use below code-

arrValues.where(function(d) d mod 2=0).Select(function(d) d*d).sum

Note:- We cannot use sum in the for each loop as it will not return the Ienumerable of collections, it will return only double.

Rest of the above code will remain same, we will use logmessage to print the sum.

Result:-
image

Example 3:- Print the values that are greater than 10.

Result:-
image

We could write same syntax in Query Method as well.

(From item in arrValues where item mod 2=0 select item*item)

Example 4:- Find unmatching values between 2 arrays.

To find unmatching values ,we will use the method “Except”

arrValues=new int32(){1,2,3,4,5,6,4,5}

arrValues1=new int32(){1,2,5,6,1}
image

Here, the unmatching values are 3,4

Result:-
image

Example 5:- Find matching values between 2 arrays.

To find matching values ,we will use the method “Intersect”

Result:-
image

Example 6:- Remove duplicates from the array.

To Remove duplicates ,we will use the method “Distinct”.

Result:-
image

Example 6:- Get distinct values from both the arrays.

To Get distinct values from both the arrays.,we will use the method “Union”.
image
Result:-
image

I hope you enjoyed the article.

Happy Automation!!

3 Likes