Linq query to remove matched rows

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))

@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

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,

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:

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

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.