# Filter the Datatable using Linq

**URL:** <https://forum.uipath.com/t/filter-the-datatable-using-linq/649646>\
**Category:** Studio\
**Tags:** project-panel, question, studio\
**Created:** [October 19, 2023, 9:00am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646 "2023-10-19T09:00:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SRILAHARI\_8](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/srilahari_8/32/431394_2.png) [@SRILAHARI\_8](https://forum.uipath.com/u/SRILAHARI_8)\
**Post date:** [October 19, 2023, 9:00am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646/1 "2023-10-19T09:00:59Z")

</div>

How to filter data in datatable1 based on the condition of datatable2 using LINQ?  
get the solution for this problem.

---

<div class="post-metadata">

**Author:** ![Shiva\_Nikhil](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/shiva_nikhil/32/267408_2.png) [@Shiva\_Nikhil](https://forum.uipath.com/u/Shiva_Nikhil)\
**Post date:** [October 19, 2023, 9:02am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646/2 "2023-10-19T09:02:40Z")

</div>

@SRILAHARI_8

can you provide what was your input and expected output

---

<div class="post-metadata">

**Author:** ![AutomationX\_by\_Kiran](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/automationx_by_kiran/32/462564_2.png) [@AutomationX\_by\_Kiran](https://forum.uipath.com/u/AutomationX_by_Kiran)\
**Post date:** [October 19, 2023, 9:05am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646/3 "2023-10-19T09:05:38Z")

</div>

Hi @SRILAHARI_8 ,

Please test the below linq query

var dt\_result = dt1.AsEnumerable()  
.Where(r =\> dt2.AsEnumerable()  
.Any(r2 =\> r.Field(“Column1”) == r2.Field(“Column1”)))  
.CopyToDataTable();

Here’s a breakdown of what the code does:

1. `dt1.AsEnumerable()`: This converts the rows of DataTable `dt1` into an enumerable collection of DataRow objects. This is necessary because LINQ queries operate on enumerable collections.
2. `dt2.AsEnumerable()`: Similarly, this converts the rows of DataTable `dt2` into an enumerable collection of DataRow objects.
3. `.Where(r => dt2.AsEnumerable().Any(r2 => r.Field<string>("Column1") == r2.Field<string>("Column1")))`: This is the core of the LINQ query. It filters the rows of `dt1` based on a condition. The condition is specified by a lambda function `r => dt2.AsEnumerable().Any(r2 => r.Field<string>("Column1") == r2.Field<string>("Column1"))`, which returns true for rows of `dt1` where the value in the “Column1” field is equal to the value in the “Column1” field of any row in `dt2`.
4. `.CopyToDataTable()`: This converts the filtered enumerable collection of DataRow objects back into a DataTable

Regards  
Happy Automation

---

<div class="post-metadata">

**Author:** ![Shiva\_Nikhil](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/shiva_nikhil/32/267408_2.png) [@Shiva\_Nikhil](https://forum.uipath.com/u/Shiva_Nikhil)\
**Post date:** [October 19, 2023, 9:06am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646/4 "2023-10-19T09:06:55Z")

</div>

@SRILAHARI_8

you can refer this

> [@Want to select multiple items from matching row](https://forum.uipath.com/t/want-to-select-multiple-items-from-matching-row/644524/2):
>
> @PPIM_RPA dt2.Asenumerable.where(function(x) dt1.Asenumerable.any(function(r) r(“OA Name”).tostring.trim.equals(x(“OA Name”).tostring.trim)).copytodatatable have you tried this

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [October 22, 2023, 9:07am UTC](https://forum.uipath.com/t/filter-the-datatable-using-linq/649646/5 "2023-10-22T09:07:17Z")

</div>

Closed on October 22, 2023, 9:07am UTC

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