If you want to compare datatable and find unmatched datarows in datatable. For this purpose we will be using LINQ to Datatable For comparing data in datatable both the datatables schema should be same.
Below is your Datatable A
StudID Name City
1 Rohan Pune
2 Sachin Mumbai
3 Vinod Delhi
4 Andy Kanpur
5 Neha Nagpur
Datatable B
StudID Name City
1 Rohan Pune
2 Sachin Hyderabad
3 Andy Delhi
4 Andy Kanpur
Now we will compare Table A and Table B and get unmatched data from A. This should give us rows with StudentID 2,3,5.
Now the datatable dtUncommon has uncommon datarows from datatable A.
Now we will compare Table A and Table B and get unmatched data from Datatable B. This should give us rows with StudentID 2,3.
Now the datatable dtUncommon has uncommon datarows from datatable B.
Below is your Datatable A
StudID Name City
1 Rohan Pune
2 Sachin Mumbai
3 Vinod Delhi
4 Andy Kanpur
5 Neha Nagpur
Datatable B
StudID Name City
1 Rohan Pune
2 Sachin Hyderabad
3 Andy Delhi
4 Andy Kanpur
Now we will compare Table A and Table B and get unmatched data from A. This should give us rows with StudentID 2,3,5.
IEnumerable dtAEnum = dtA.AsEnumerable();
IEnumerable dtBEnum = dtB.AsEnumerable();
DataTable dtUncommon = dtAEnum.Except(dtAEnum).CopyToDataTable();
Now the datatable dtUncommon has uncommon datarows from datatable A.
Now we will compare Table A and Table B and get unmatched data from Datatable B. This should give us rows with StudentID 2,3.
DataTable dtUncommon = dtBEnum.Except(dtAEnum).CopyToDataTable();
add using System.Linq namespace to access Except() method.Now the datatable dtUncommon has uncommon datarows from datatable B.
Comments
Post a Comment