How to find common rows between two datatables using LINQ?

heading common rows in excel?
how to find common rows between two datatables using LINQ?
please give best topic

1 Like

@Sravani_Reddy

refer this

Hi @Sravani_Reddy ,

You could maybe check with the below post :

Additionally, If Specific column match is required, we could use Join Datatables Activity and if not the expected output we could go for a Linq Join

@Sravani_Reddy

variable_name= dt1.AsEnumerable
    .Intersect(dt2.AsEnumerable, DataRowComparer.Default);

cheers…!

@Sravani_Reddy

Use this Linq Query

from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable() on r1[“ID”] equals r2[“ID”]
where r1[“Name”].Equals(r2[“Name”])
select r1;

Hi @Sravani_Reddy ,

Use the below QUery

var Dt_commonRows = dt1.AsEnumerable()
.Intersect(dt2.AsEnumerable(), DataRowComparer.Default)
.CopyToDataTable();

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.

dt2.AsEnumerable() : Similarly, this converts the rows of DataTable dt2 into an enumerable collection of DataRow objects.

.Intersect(dt2.AsEnumerable(), DataRowComparer.Default) : This is the core of the LINQ query. The Intersect method is used to find the common rows between the two DataTables. DataRowComparer.Default is used as the comparer to compare DataRow objects. It compares all fields in the DataRow objects for equality.

.CopyToDataTable() : This converts the resulting enumerable collection of DataRow objects back into a DataTable.

Regards
Happy Automation

Check this out for all datatable functionalities

Cheers @Sravani_Reddy