Check if row exists in DataTable?

Curious picture Curious · Sep 28, 2014 · Viewed 58.9k times · Source

I have a datatable and a row. I want to import the row to the datatable only if it does not exist in the datatable.

How can i do that?

Answer

S22 picture S22 · Sep 28, 2014

If you use a typed DataSet, I.e. declared in design time, the "linq Contains method" takes a typed DataRow. The default IEqualityComparer will compare all values in the DataRow. (Which is normally useless, since you should have a key defined).

DataSet1 ds = new DataSet1();
DataSet1.DataTable1Row row = ds.DataTable1.AddDataTable1Row(bla, bla);
bool exists = ds.DataTable1.Contains(row);