7

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
  • 175,020
  • 35
  • 237
  • 263
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138

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
  • 175,020
  • 35
  • 237
  • 263
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,670
  • 12
  • 48
  • 59