How to filter a datatable using Linq

hi Team,

In my project, I am having a datatable like below: dt_test

I want to count of the rows where Age = “20”

I have been trying to use below logic (seems correct): LINQ operation

But I am getting error sayign that value row is not declared.

i am using namespaces:

  1. System.Linq
  2. System.Data
  3. System.Data.DatesetExtension.

Experts please helps @singh_sumit @ashokkarale @jast1631

Hi @hacky

Count = DT.AsEnumerable().Where(Function(row) row("Age").ToString() = "20").Count()

Regards,

And the Where Operator

So let us know if you need C# or VB.Net Syntax

@hacky,

You can use a LINQ query in UiPath to count the rows in a DataTable where the age is equal to 20. Here is how you can write it:

Assuming:

  • Your DataTable is named dt.
  • The column name for age is Age.

The LINQ query would be:

countRows = dt.AsEnumerable().Count(Function(row) row.Field(Of Integer)("Age") = 20)

Explanation:

  • AsEnumerable() converts the DataTable to an enumerable collection so that LINQ can be applied.
  • Field(Of Integer)("Age") gets the value of the “Age” column for each row.
  • .Count(Function(row) ...) counts the rows where the condition (age = 20) is met.

Make sure that the “Age” column is of type Integer. If it’s of another type, like String, you can modify the query accordingly:

countRows = dt.AsEnumerable().Count(Function(row) row.Field(Of String)("Age") = "20")

This query will return the count of rows where the age is 20.

LLM helped me write this but it’s validated by me.

Thanks,
Ashok :slightly_smiling_face:

Hie @hacky check this
your input
The image shows a "Build Data Table" window with a column labeled "Value (String)" containing various numerical values as strings, and options to add or delete rows. (Captioned by AI)
note : Count is a integer variable


Output
A data table with six numeric values and a message box displaying the number "3" is shown on a computer screen. (Captioned by AI)

cheers Happy Automation

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