Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of two DataTables in c#

Tags:

c#

linq

datatable

I have two data tables as follows

dtOne
-------------------------
  ID  |   Name 
--------------------------
 101  |  ABC
 102  |  XYZ
 103  |  MNO
--------------------------

dtTwo
-------------------------
  ID  |   Name 
--------------------------
 101  |  ABC
 102  |  XYZ
--------------------------

I just want the result as data which is in dtOne and not in dtTwo (dtOne-dtTwo)

dtResult
-------------------------
  ID  |   Name 
--------------------------
 103  |  MNO
--------------------------

How can i achieve this .

like image 444
Nithesh Narayanan Avatar asked Feb 15 '12 07:02

Nithesh Narayanan


2 Answers

TO get it work its better to use Linq To DataSet will resolve it easily..

DataTable table1= ds.Tables["table1"];
DataTable table2= ds.Tables["table2"];
var diff= table1.AsEnumerable().Except(table2.AsEnumerable(),
                                                    DataRowComparer.Default);
like image 102
Pranay Rana Avatar answered Nov 14 '22 02:11

Pranay Rana


Starting with the solution showed under LINQ query on a DataTable, I'd try it with:

var dtOneData = from myRow in dtOne.AsEnumerable();
var dtTwoData = from myRow in dtOne.AsEnumerable();
var difference = dtOneData.Except(dtTwoData);
like image 39
bernhardrusch Avatar answered Nov 14 '22 03:11

bernhardrusch