Filtering data table by using a LINQ query's

Filtering DataTable Rows based on Conditions

Hello UiPath community,

I’m trying to filter rows in a DataTable using LINQ based on certain conditions, and I’m having trouble constructing the right query. Specifically, I want to extract rows where the value in column “Status” is “Completed” and the value in column “Priority” is greater than 3. Could someone guide me on constructing a LINQ query to achieve this in UiPath Studio?

Hi @killarapusiva ,

// Assuming dt is your DataTable
var filteredRows = from row in dt.AsEnumerable()
where row.Field(“Status”) == “Completed” &&
row.Field(“Priority”) > 3
select row;

// Convert the result to a new DataTable or use further processing
DataTable resultDataTable = filteredRows.CopyToDataTable();

This LINQ query selects rows from the DataTable where the “Status” column is “Completed” and the “Priority” column is greater than 3. The CopyToDataTable() method is then used to convert the result into a new DataTable.

Happy Automation

1 Like

Hi @killarapusiva

Try this query:

dtFiltered = dt.AsEnumerable().Where(Function(row) row.Field(Of String)("Status") = "Completed" AndAlso row.Field(Of Integer)("Priority") > 3).CopyToDataTable()

' dtFiltered is now your filtered DataTable

Explanation:

  1. dt.AsEnumerable(): This converts the DataTable (dt) into an enumerable collection of DataRow. This is necessary because LINQ operates on IEnumerable collections.
  2. .Where(Function(row): This is the filtering condition. It iterates through each row in the DataTable and checks if the conditions inside the Function are met.
  3. row.Field(Of String)("Status") = "Completed": This checks if the value in the “Status” column of the current row is equal to “Completed”. It’s using the Field method to access the value in a specific column.
  4. AndAlso: This is a logical operator that performs a logical AND operation. It ensures that both conditions must be true for a row to be included in the result.
  5. row.Field(Of Integer)("Priority") > 3: This checks if the value in the “Priority” column of the current row is greater than 3.
  6. .CopyToDataTable(): This converts the filtered rows back into a DataTable. It’s necessary because the result of the Where clause is an IEnumerable<DataRow>, and CopyToDataTable is used to convert it back to a DataTable.

Regards

1 Like

Hi @killarapusiva

Can you try the following?

dt_Input.Select("Status = 'Completed' AND Priority > 3").CopyToDataTable()

I am assuming the input as below

and the output is below

image

Cheers!!

1 Like

Thanks for sharing this with me

@killarapusiva

If you found the solution for your question can you please mark it as solution so it will be helpful for others.

Thanks for sharing this

Thanks for sharing this .it’s working

1 Like

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