8

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 .

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
Nithesh Narayanan
  • 10,803
  • 33
  • 93
  • 136

2 Answers2

20

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);
Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
2

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);
Community
  • 1
  • 1
bernhardrusch
  • 11,380
  • 12
  • 45
  • 58