niro
August 5, 2024, 2:09pm
1
Hi, can you help me to build linq query to remove rows from a datatable if this condition matches? Filter datatable is not working as expected for this condition
Remove IF (Country = ‘Germany’ AND (Grade = 6 or Grade = 7 or Grade = 9) AND (Bonus = ‘A Plan’ or ‘B Plan’ or 'C Plan))
rlgandu
(Rajyalakshmi Gandu)
August 5, 2024, 2:11pm
2
@niro
dtFiltered = dtOriginal.AsEnumerable()
.Where(Function(row) Not (row.Field(Of String)("Country") = "Germany" And
(row.Field(Of Integer)("Grade") = 6 Or
row.Field(Of Integer)("Grade") = 7 Or
row.Field(Of Integer)("Grade") = 9) And
(row.Field(Of String)("Bonus") = "A Plan" Or
row.Field(Of String)("Bonus") = "B Plan" Or
row.Field(Of String)("Bonus") = "C Plan")))
.CopyToDataTable()
Note:Please change the datatype of your Columns Sometimes it may be Double or it may be String for Grade column
lrtetala
(Lakshman Reddy)
August 5, 2024, 2:16pm
3
Hi @niro
filteredDataTable = dataTable.AsEnumerable().
Where(Function(row) Not (row("Country").ToString() = "Germany" AndAlso
(row("Grade").ToString() = "6" OrElse row("Grade").ToString() = "7" OrElse row("Grade").ToString() = "9") AndAlso
(row("Bonus").ToString() = "A Plan" OrElse row("Bonus").ToString() = "B Plan" OrElse row("Bonus").ToString() = "C Plan"))).
CopyToDataTable()
Regards,
ppr
(Peter Preuss)
August 5, 2024, 2:17pm
4
Can you try:
Assign Activity
dtResult =
(From d in yourDataTableVar.AsEnumerable
Where Not d("Country").toString.ToUpper.Trim.Equals("GERMANY")
Where Not {"6","7","8"}.Contains(d("Grade").toString.Trim)
Where Not {"A Plan","B Plan","C Plan"}.Contains(d("Bonus").toString.Trim)
Select r = d).CopyToDataTable
Handling empty filter result:
This FirstAid Tutorial will describe how a the source contains no DataRows EXCEPTION can be handled.
Introduction
Let’s have a look at the following filter data table scenario:
Name
CCode
Tom
US
Charlotte
FR
Seema
IN
Jean
FR
Assignment:
Filter all rows on a particular Country Code like FR, UK, ES, UK…
Ensure that all CCode data column values are trimmed
Ensure that the Filter check is case insensitive
we can implement it e.g. with the help of a LINQ statement:
dtData.AsE…
ppr
(Peter Preuss)
August 5, 2024, 2:23pm
5
niro:
build linq query
For LINQ Learning have a look here:
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum
In general we recommend also:
do not over-LINQ statements
avoid redundant parts within a LINQ whenever a construct can be expressed different
system
(system)
Closed
August 8, 2024, 2:23pm
6
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.