Filter Table with LINQ

Hi Experts,

I have an Excel file with contain column as Differentiate and i need filter this to get filter differentiate amount only plus 5 and minus 5 in it using LINQ filter method. Please suggest me to achieve this.

Regards,
Balachander
Input.xlsx (9.1 KB)

@Balachander_Pandian

dtExcel.AsEnumerable().Where(Function(row) row(2).ToString.Equals(“-5”) or row(2).ToString.Equals(“5”)).CopyToDataTable()

@Balachander_Pandian

Try this:

(From row In YourDataTableVar.AsEnumerable()
 Where (row.Field(Of Integer)("Differenciate") = 5 Or row.Field(Of Integer)("Differenciate") = -5)
 Select row).CopyToDataTable()

@Balachander_Pandian

Try this Linq

from row in excelDT
where row.Differentiate == plus5 || row.Differentiate == minus5
select row;

Hi @Balachander_Pandian

(From row In dt.AsEnumerable()
                    Where row.Field(Of Double)("Differenciate") = 5 Or row.Field(Of Double)("Differenciate") = -5
                    Select row).CopyToDataTable

Hope it helps!!

Hi @Balachander_Pandian

If you want only 5 and -5 then try below code

(From row In DT1.AsEnumerable()
Where (CInt(row("Differenciate")) = 5 Or CInt(row("Differenciate")) = -5)
Select row).CopyToDataTable

If you want -5 to 5 values then try below code

(From row In DT1.AsEnumerable()
              Let differentiate = row.Field(Of Double)("Differenciate")
              Where differentiate >= -5 AndAlso differentiate <= 5
              Select row).CopyToDataTable()

image

O/P:

Input (1).xlsx (10.3 KB)

Hope this helps!!

(From d in dtData.AsEnumerable
Let val = CDbl(d(“Differnciate”).toString.Trim
where Math.Abs(val).Equals(5)
Select r = d).CopyToDataTable

Handling empty filter result
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

Please let us know if =5 or =-5 is needed or the range -5 to 5 is needed

@ppr need range between -5 to 5 is needed

@Balachander_Pandian

Try this

(From row In DT1.AsEnumerable()
              Let differentiate = row.Field(Of Double)("Differenciate")
              Where differentiate >= -5 AndAlso differentiate <= 5
              Select row).CopyToDataTable()

@lrtetala I have tried this but didn’t meet the expected output as sheet3 … I am expecting output as sheet3 which you had done from your end.

Hereby i attached my output . Please advise me
Output.xlsx (29.6 KB)

Hi @Balachander_Pandian

(From row In dt.AsEnumerable()
                    Let differentiateValue = row.Field(Of Double)("Differenciate")
                    Where differentiateValue >= -5 AndAlso differentiateValue <= 5
                    Select row).CopyToDataTable


Output

image

Hope it helps!!

@Balachander_Pandian

(From row In DT1.AsEnumerable()
              Let differentiate = row.Field(Of Double)("Differenciate")
              Where differentiate >= -5 AndAlso differentiate <= 5
              Select row).CopyToDataTable()

O/P:

Output.xlsx (135.0 KB)

@Balachander_Pandian

Check this output for your recently updated file

Output (2).xlsx (40.7 KB)

changed to:

(From d in dtData.AsEnumerable
Let val = CDbl(d(“Differnciate”).toString.Trim
where Math.Abs(val) <= 5
Select r = d).CopyToDataTable