Linq Notes
Linq Notes
No Topic
1 Linq
3 Namespace to import
4 Method Syntax:
5 Query Syntax
Query Syntax
Method Syntax
Query Syntax
Mixed Syntax
Mixed Syntax
11 .First Method
Syntax
Method Syntax
12 .Where Method
14 .Select Method
Query Breakdown:
16 .GroupBy Method
Query to group by Item Column and get output in datatable
17 .Count Method
18 .Sum Method
21 Average Method
Scenario: You are working on a UiPath automation project where you have
two DataTables: EmpDt and DeptDt. Your task is to perform a join operation
using LINQ on these DataTables based on the 'DeptID' column and create a
new DataTable named OutputDt.
Query for Additionally, check the accuracy of the second rumor suggesting
that "all employees received bonus $15,000.
SkipWhile Method
TakeWhile Method
Scenario:
As a UiPath RPA Developer, You are tasked with identifying the
"Silver Star Performers" from the given StudentsDt table using
UiPath LINQ.
Use UiPath LINQ to perform the following tasks:
a. Sort the students by their marks in ascending order.
b. Skip students with marks less than 80.
C. Take students until you encounter a student with marks
less than 90.
As a UiPath RPA Developer, you are tasked with data cleansing operations
using LINQ. Frame LINQ queries to achieve the following tasks:
a. Utilize LINQ to remove duplicate Row-Wise
b: Remove duplicates from a ProductName Column
It allows developers to query and manipulate data from different sources directly within their .NET-based applicatio
1. Method Syntax
2. Query Syntax
System.linq
Utilizes method chaining with dot notation and Lambda expressions to perform operations.
We use the Where, OrderBy, Select, and ToArray methods in a chained manner.
arrTest.Where(Function(p)p>5).Select(Function(p) p*p)
Query syntax is similar to SQL (Structured Query Language) The LINQ query syntax starts with "From" keyword and e
From p in arrTest
Where p>5
Select p*p
dt_Input.AsEnumerable().Where(Function(row) row("Total").ToString.Contains("USD")).CopyToDataTable
(From row In dt_Input
Where row.field(Of String)("Total").contains("USD")
Select row).copyToDatatable
For multiple condition use AndAlso keyword to add multiple condition within where clause
Here only digits sholud be present in Price column and currency symbols to be removed
CollectionName.First()
CollectionName.First(condition)
Here First value from column 'Quantity' is retrieved after applyinf filter to Item' column
dt_Input.AsEnumerable().First(Function(row) row("Item").ToString.Equals("Orange"))("Quantity").ToString
The primary goal of the .Where method is to filter elements based on a specified condition.
dt.Where(Lambda Function Filtering Condition).ToArray/ToList/. - Returns: IEnumerable<T> sequence, where T is th
dealing with
dt_Output.AsEnumerable().Where(Function(x) x("Item").ToString.Equals("Mango")).ToList
How can you retrieve the quantity value for the second occurrence of the item "mango" from a DataTable in UiPa
correct syntax:
A. dt.AsEnumerable.Where(Function(x) x("Item").ToString.Equals("Mango"))(1)("Quantity").ToString
B. dt.Select("Item = 'Mango'")(1)("Quantity").ToString
Purpose of .Select Method: The .Select method in LINQ is used to transform elements in a collection
What it Returns: The .Select method returns a new collection containing the transformed elements. The type of the
collection may be different from the type of the elements in the original collection, depending on the transformatio
intArray.Select(Function(x) x*x).ToArray
For Linq Query Collection needs to be converted to Ienumerable collection to iterate so cast method is used
Or
The Cast method is used to convert the Columns collection to an IEnumerable of System.Data.DataColumn. This is
Columns collection is not inherently an IEnumerable of DataColumn, and LINQ methods require IEnumerable.
dt_Output.Columns.Cast(of System.Data.DataColumn): Converts the Columns collection to an IEnumerable of DataC
dt_Output.Columns.Cast(of System.Data.DataColumn).Select(Function(x) x.ColumnName): Projects each DataColum
property, creating a sequence of column names.
dt_Output.AsEnumerable() :
Converts the DataTable (dt_Output) to an IEnumerable(Of DataRow). This allows you to use LINQ methods on the D
dt_Output.AsEnumerable().GroupBy(Function(x) x("Item").ToString) :
Returns output: IGrouping [ String, Datarow]
IGrouping is linq datataype contains Key and Element.
It stands for Interface(a group which has a commom key) Grouping.
IGrouping output contains multiple collections hence cannot directly be added to datatable. Fot this SelectMany M
collections into 1 collection. Flattens the groups back into a single sequence of DataRows.
dt_Output.AsEnumerable().GroupBy(Function(x) x("Item").ToString).SelectMany(Function(grp) grp):
This returns IEnumerable of Datarows
1. (Sum, Count, Min, Max, Average, and Aggregate) are typically referred to as LINQ Aggregate Functions or LINQ Ag
2. The Count method is used in LINQ to determine the number of elements in collections like Arrays, Lists, or DataTa
3. It returns an integer representing the count of elements in the collection.
4. It can be used without parameters to count all elements in the sequence.
5. It can also be used with a predicate (lambda expression) to count elements that satisfy a specific condition.
dt_Output.AsEnumerable().Count(Function(x) x("Dept").ToString.Trim.Equals("IT"))
dt_Output.AsEnumerable().Count(Function(x) x("Dept").ToString.Trim.Equals("IT") Or x("Dept").ToString.Trim.Equal
dt_Output.AsEnumerable().Count(Function(x) x("Dept").ToString.Trim.Equals("IT") And CInt(x("Salary")) > 20000)
dt_Output.AsEnumerable().Count(Function(x) CInt(x("Salary")) >= 20000 And x("Dept").ToString.Trim.Equals("IT") O
x("Dept").ToString.Trim.Equals("HR"))
dt_Output.AsEnumerable().Sum(Function(x) CInt(x("Salary")))
dt_Input.AsEnumerable().OrderBy(Function(x) CInt(x("Salary"))).CopyToDataTable
dt_Input.AsEnumerable().OrderByDescending(Function(x) CInt(x("Salary"))).CopyToDataTable
The Skip and Take methods in LINQ are used to manipulate sequences by skipping a specified number of elements a
number of elements from the resulting sequence.
dt_Input.AsEnumerable().Skip(RecordsToSkip).Take(ExportToTake).CopyToDataTable
RecordsToSkip: Int variable indicating the records to skip from datatable
ExportToTake: Int variable mentioning the records to get after skipping in output datatable
dt_Emp.AsEnumerable().Join(dt_Dept.AsEnumerable(),
Function(emp) emp("DeptID").ToString,
Function(dept) dept("DeptID").ToString,
Function(emp,dept) New Object() {
emp("ID").ToString,
emp("Name").ToString,
emp("DeptID").ToString,
emp("Salary").ToString,
dept("DeptName").ToString
}
).Select(Function(x) dt_Output.Rows.Add(x)).CopyToDatatable
The SkipWhile method bypasses elements in a sequence as long as a specified condition is true and then returns the
The TakeWhile method returns elements from a sequence as long as a specified condition is true and then skips the
dt_Input.AsEnumerable().OrderBy(Function(x) CInt(x("Marks"))). _
SkipWhile(Function(y) CInt(y("Marks")) < 80). _
TakeWhile(Function(z) CInt(z("Marks")) < 90).CopyToDataTable
dt_Input.DefaultView.ToTable(True)
Sheet1
Sheet1
Sheet2
dt_Clone is assigned before as
dt_Input.Clone to have schema of
dt_Input beforehand
Sheet2
Sheet2
dt_NewClone is assigned before as
dt_Input.Clone to have schema of
dt_Input beforehand
Sheet2
Sheet3
Sheet3
Sheet4
Sheet5
Sheet5
Sheet5
Sheet5
dt_Output is output table and
output of build datatable activity
which contains column names of
final table.
Join Method Needs Two Datatables. Both
Query Breakdown: datatables are available in same sheet (Sheet6)
1. `dt_Emp.AsEnumerable()`
- Converts the `dt_Emp` DataTable
to an `IEnumerable(Of DataRow)`,
allowing LINQ operations on the
employee rows.
2. `Join` Method
- Joins `dt_Emp` with `dt_Dept`
based on a common key (DeptID).
Sheet7
Sheet8
Sheet8
Sheet9
Invoice Number Vendor Tax ID Invoice Item Total Date
772505 FR453231 Beverages and Catering 207366 CAD 1/4/2018
294814 FR322345 Professional Services 293110 USD 0000-00-00
526762 DE763212 Professional Services 128440 CAD 1/23/2018
611418 RO123456 IT Support 314539 USD 1/1/2018
346645 RU567434 Waste management services 131551 RON 1/5/2018
847673 RO254678 Beverages and Catering 86726.4 USD 1/12/2018
582634 RO254678 Professional Services 42578.4 CAD 1/11/2018
755412 FR322345 Beverages and Catering 299098 USD 1/23/2018
611271 RO123456 Waste management services 6573.6 EUR 1/6/2018
847081 RO657483 Professional Services 122314 USD 1/24/2018
756850 FR329083 Various paper supplies 355907 EUR 1/23/2018
892803 RO345879 Professional Services 13620 CAD 1/7/2018
332647 RO657483 Waste management services 223289 CAD 1/11/2018
885235 RO892123 Concierge Services 313955 RON 1/18/2018
256742 FR453231 Waste management services 308678 EUR 1/24/2018
476777 FR121212 Various paper supplies 117454 CAD 1/12/2018
812384 DE456232 IT Support 323039 USD 1/15/2018
493707 RO254678 IT Support 272568 CAD 2/17/2018
920044 RO125678 Beverages and Catering 44926.8 USD 2/5/2018
986127 RO254678 Professional Services 306724 USD 2/1/2018
697367 RO345879 Concierge Services 194696 CAD 2/13/2018
921288 RO874231 IT Support 90056.4 RON 2/2/2018
620467 RO094782 Waste management services 115828 USD 2/12/2018
869949 FR329083 IT Support 317084 RON 2/17/2018
646411 IT231232 Professional Services 171294 EUR 2/2/2018
535279 FR453231 IT Support 225155 RON 2/20/2018
920224 RO123456 Beverages and Catering 294131 RON 2/14/2018
279800 RO874231 Waste management services 278731 EUR 2/27/2018
936984 FR065748 IT Support 319678 RON 3/18/2018
767597 DE767565 Concierge Services 232387 USD 0000-00-00
559197 RO892123 Beverages and Catering 308653 EUR 3/12/2018
131547 FR121212 Concierge Services 347954 EUR 3/15/2018
619050 RO094782 Concierge Services 66308.4 USD 3/11/2018
382748 FR453231 Concierge Services 84813.6 CAD 3/22/2018
438114 DE987564 Concierge Services 343296 CAD 3/23/2018
848754 FR329083 Concierge Services 105476 EUR 3/2/2018
918755 FR453231 Professional Services 47328 CAD 3/23/2018
827887 DE456232 Professional Services 338575 USD 3/9/2018
982472 RO345879 Various paper supplies 351361 RON 3/1/2018
103330 DE767565 IT Support 121226 USD 4/16/2018
255674 DE987564 Concierge Services 246604 USD 4/12/2018
572952 RU567434 IT Support 163369 USD 4/26/2018
982477 DE456232 Professional Services 169777 RON 4/19/2018
903478 DE763212 Beverages and Catering 24088.8 CAD 4/22/2018
575614 DE987564 Waste management services 18776.4 CAD 4/16/2018
592820 RO125678 IT Support 24519.6 USD 4/1/2018
376079 DE456232 Beverages and Catering 112652 USD 4/22/2018
961173 RO125678 Waste management services 296689 USD 4/3/2018
691700 FR121212 Beverages and Catering 206618 CAD 4/2/2018
226521 RO892123 Beverages and Catering 181168 USD 4/10/2018
216486 FR329083 Waste management services 280109 CAD 4/8/2018
200103 RO657483 Various paper supplies 116555 RON 4/28/2018
630375 RU567434 Various paper supplies 70675.2 EUR 0000-00-00
876265 FR065748 Professional Services 231450 EUR 4/16/2018
153905 RO123456 Professional Services 39906 USD 4/18/2018
462171 DE987564 Various paper supplies 332346 RON 4/23/2018
636711 FR453231 Concierge Services 94351.2 RON 5/12/2018
860653 RO254678 Various paper supplies 250206 EUR 5/23/2018
152756 RO345879 Various paper supplies 81816 USD 5/21/2018
290969 FR065748 Various paper supplies 339635 CAD 5/9/2018
508272 RU567434 Concierge Services 295417 CAD 5/6/2018
710052 DE456232 Professional Services 163344 CAD 5/27/2018
618548 RO123456 IT Support 97717.2 CAD 5/14/2018
759111 RO125678 Various paper supplies 248900 CAD 5/22/2018
290965 FR453231 Professional Services 161191 EUR 5/23/2018
556596 RO892123 Waste management services 267888 CAD 5/6/2018
603712 FR065748 Various paper supplies 230236 EUR 5/24/2018
851282 FR121212 Waste management services 21979.2 RON 5/3/2018
864114 IT213456 Various paper supplies 107496 RON 5/5/2018
610519 RO094782 Various paper supplies 10476 RON 5/9/2018
400508 DE456232 Beverages and Catering 159652 USD 5/16/2018
980014 FR322345 Waste management services 268794 USD 5/21/2018
344682 RO345879 IT Support 248024 EUR 5/13/2018
113840 DE763212 IT Support 136770 CAD 5/6/2018
300783 FR453231 Various paper supplies 64492.8 CAD 6/24/2018
558091 RO123456 Professional Services 157674 RON 6/21/2018
125423 FR065748 Professional Services 31753.2 USD 6/18/2018
650639 IT213456 Concierge Services 260776 CAD 6/12/2018
373058 FR453231 Professional Services 14484 RON 6/5/2018
873014 RO657483 Various paper supplies 46416 RON 6/2/2018
676842 DE767565 Professional Services 1495.2 EUR 6/2/2018
630486 FR065748 Waste management services 173783 USD 6/26/2018
759861 DE456232 Professional Services 12618 EUR 6/6/2018
196968 RO125678 Professional Services 270600 RON 7/17/2018
488303 FR065748 Professional Services 55161.6 EUR 7/2/2018
454535 RO874231 Concierge Services 62494.8 CAD 7/10/2018
719442 FR322345 Concierge Services 326531 USD 7/27/2018
104518 FR065748 Professional Services 183262 USD 7/9/2018
601013 DE456232 Waste management services 120667 EUR 7/5/2018
761868 RO657483 Various paper supplies 323684 RON 7/21/2018
892186 FR121212 Concierge Services 168968 USD 7/23/2018
108557 FR065748 IT Support 32278.8 EUR 7/3/2018
386198 FR065748 IT Support 96532.8 CAD 7/12/2018
671988 DE767565 Waste management services 178685 EUR 7/2/2018
562615 RO123456 IT Support 9873.6 USD 7/19/2018
236684 RO254678 IT Support 44529.6 EUR 8/22/2018
845828 RO123456 Waste management services 248569 USD 8/1/2018
538704 RO874231 IT Support 184118 RON 8/7/2018
698687 RO874231 Concierge Services 193709 CAD 8/26/2018
537441 RO123456 Waste management services 179162 USD 8/28/2018
604097 FR322345 Concierge Services 56707.2 CAD 8/13/2018
637436 FR065748 Various paper supplies 39171.6 RON 8/4/2018
491404 IT231232 Professional Services 10680 USD 8/22/2018
181412 RO892123 Concierge Services 329514 USD 8/22/2018
217964 FR065748 Waste management services 91165.2 CAD 8/4/2018
325102 FR322345 Beverages and Catering 198218 RON 8/28/2018
757294 DE763212 Waste management services 83823.6 USD 8/14/2018
779906 IT213456 Concierge Services 332700 USD 8/11/2018
757943 RO125678 Waste management services 236276 RON 8/25/2018
148542 FR065748 Various paper supplies 301326 CAD 9/25/2018
931373 RO874231 IT Support 132848 RON 9/5/2018
445281 IT231232 Waste management services 105229 CAD 9/18/2018
806961 RO125678 Various paper supplies 5884.8 RON 9/1/2018
700965 RO123456 Various paper supplies 301936 USD 9/14/2018
303407 DE987564 Beverages and Catering 253500 USD 9/19/2018
466312 RO094782 Waste management services 160276 USD 9/3/2018
380805 DE767565 Various paper supplies 9219.6 EUR 9/22/2018
451314 FR453231 Concierge Services 75781.2 CAD 9/1/2018
575352 DE763212 Professional Services 146233 USD 9/20/2018
970961 FR453231 Concierge Services 264473 CAD 10/25/2018
358715 FR121212 Waste management services 21704.4 USD 10/19/2018
797269 RO892123 Waste management services 205259 RON 10/16/2018
460429 RO892123 Waste management services 338105 USD 10/27/2018
768651 IT213456 IT Support 176495 EUR 10/15/2018
791627 RO657483 Professional Services 350636 EUR 10/18/2018
760896 IT231232 IT Support 64512 EUR 10/25/2018
629989 FR453231 Professional Services 114800 RON 10/11/2018
449755 FR322345 IT Support 94647.6 EUR 10/21/2018
344281 IT213456 IT Support 305882 EUR 10/5/2018
496746 RO123456 Waste management services 50283.6 RON 10/11/2018
238777 FR453231 Concierge Services 157884 RON 10/22/2018
546837 RO657483 Various paper supplies 88921.2 USD 11/19/2018
433966 FR322345 IT Support 51478.8 USD 11/6/2018
582821 DE767565 Concierge Services 258648 CAD 11/24/2018
387560 RO094782 Various paper supplies 286405 EUR 11/25/2018
683803 RU567434 Waste management services 245321 USD 11/1/2018
652588 DE763212 Various paper supplies 292903 USD 11/12/2018
224860 RU567434 Various paper supplies 102050 USD 11/27/2018
971764 FR453231 Waste management services 286816 USD 11/7/2018
738243 FR453231 Concierge Services 2782.8 USD 11/28/2018
176488 RO345879 Waste management services 113188 RON 11/17/2018
335733 FR329083 Concierge Services 175566 EUR 11/2/2018
964858 RO345879 Various paper supplies 55078.8 USD 11/2/2018
770919 DE767565 Waste management services 240910 CAD 11/4/2018
367216 RO094782 Concierge Services 182576 RON 11/15/2018
669211 DE456232 Professional Services 240163 RON 11/16/2018
283285 RO892123 IT Support 45663.6 USD 12/21/2018
126011 FR453231 Various paper supplies 196727 USD 12/22/2018
711195 FR121212 IT Support 156313 RON 12/18/2018
386025 FR065748 Waste management services 172778 RON 12/20/2018
337069 IT213456 Beverages and Catering 139420 CAD 0000-00-00
589024 RO125678 Beverages and Catering 284137 USD 12/13/2018
966386 DE763212 Professional Services 2822.4 RON 12/21/2018
999357 RU567434 Various paper supplies 141977 USD 12/16/2018
864477 FR329083 IT Support 237971 RON 12/16/2018
600328 DE763212 Various paper supplies 72805.2 CAD 12/19/2018
672858 FR322345 Various paper supplies 16018.8 CAD 12/20/2018
790938 FR121212 IT Support 134254 CAD 12/22/2018
415926 FR322345 Professional Services 194476 EUR 12/28/2018
Item Quantity Price
Apple 3 300
Mango 2 100
Orange 1 50
Item Quantity Price
Apple 3 300
Mango 2 100
Orange 1 50
Mango 10 70
No Item Quantity
1 Apple 50
2 Mango 100
3 Orange 30
4 Mango 10
5 Apple 70
6 Orange 40
ID Name Dept Salary
10001 A IT 45000
10002 B HR 15000
10003 C Finance 30000
10004 D IT 15000
10005 E HR 20000
Employee Table Dept Table
ID Name DeptID Salary DeptID DeptName
10001 A 101 45000 101 IT
10002 B 101 15000 102 HR
10003 C 102 30000 103 Finance
10004 D 103 15000
10005 E 102 20000
ID Name DeptID Bonus
10001 A 101 45000
10002 B 101 15000
10003 C 102 30000
10004 D 103 15000
10005 E 102 20000
ID Name Marks
101 John 85
102 Jane 92
103 Bob 78
104 Alice 95
105 Emily 88
106 David 75
107 Sarah 90
108 Michael 87
109 Olivia 94
110 Daniel 80
111 Chloe 89
112 Liam 82
113 Ava 91
114 Ethan 79
115 Emma 96
116 Noah 83
117 Sophia 88
118 Mason 77
ProductName Category Price
Laptop Electronics 800
Smartphone Electronics 500
Headphones Electronics 50
Laptop Electronics 800
Tablet Electronics 300
Headphones Electronics 50
Shirt Clothing 30
Smartphone Electronics 500