0

i have 2 DataTables with Data (dtm and dtl). I must find out if exist a DataRow from DataTable1 in Datatable2. I have a combined primray key of 3 columns. I know that i can get the DataTable primarykey like this.

DataColumn[] pkcol;
pkcol = dtm.PrimaryKey;

Is it possible to use the Find method like this?

if (dtl.Rows.Find(dtm[pkcol]) == null)
   {

   }

I must realize an DataTable Sync Method. So i go foreach Datarow in dtm.Rows and Foreach Datarow in dtl.Rows. It would be great if i can go ahead over the table and search for if exist the datarows primary key value in the table. Any ideas? Please help. Thanks

  • Check this thread. This will solve your problem. [LINQ query on a DataTable](http://stackoverflow.com/questions/10855/linq-query-on-a-datatable) – Shubhojit May 06 '14 at 12:15

1 Answers1

0

According to MSDN DataRowCollection.Findalready is looking for rows with the given values in the PKs. So you don't even need to get the PKs, but simply an array of values, that matches the PKs in number and type:

// Create an array for the key values to find.
object[]findTheseVals = new object[3];

// Set the values of the keys to find.
findTheseVals[0] = "John";
findTheseVals[1] = "Smith";
findTheseVals[2] = "5 Main St.";

DataRow foundRow = table.Rows.Find(findTheseVals);

You would set the values to null to find your row.

TaW
  • 53,122
  • 8
  • 69
  • 111