15

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?

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
Curious
  • 474
  • 1
  • 8
  • 25

8 Answers8

12

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);
Francesco B.
  • 2,729
  • 4
  • 25
  • 37
S22
  • 220
  • 1
  • 6
  • Well, the "LINQ contains method" referred to above is already a method extension. IF your extension was doing something useful it MIGHT have added some value to the by MS implemented extension. – S22 Dec 23 '15 at 15:21
  • I'd add.. you must have defined a primary key on the table even if you do not use a typed dataset. – Fer R Dec 29 '17 at 21:20
8

You can use LINQ to check if row is present in datatable. Follow this solution, and replace "id" with your row's primary key, by which you can uniquely identify a row in a table.

DataRow dr = null; // assign your DR here
DataTable dt = new DataTable(); // assign Datatable instance here.
var k = (from r in dt.Rows.OfType<DataRow>()  where r["id"].ToString() == dr["id"].ToString() select r).FirstOrDefault();
if(k != null)
{  // Row is present }
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
7

if you want to check all the cells in a DataRow, you can try this function:

bool ContainDataRowInDataTable(DataTable T,DataRow R)
{
    foreach (DataRow item in T.Rows)
    {
        if (Enumerable.SequenceEqual(item.ItemArray, R.ItemArray))
            return true;
    }
    return false;
}
Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40
6

you can use Contains as below

if(DataTable.Columns.Contains("RowName"))
{
  //Do some stuffs here
}
Vijay Kumbhoje
  • 1,401
  • 2
  • 25
  • 44
3

Tried all answers here but did not work, so I made something for myself which works in my case. The code is pretty simple, it checks if the row you want to add already exists in the datatable - if it does not exist in the datatable, add it.

// fill dt with information
DataTable dt = new DataTable();

// create a new row and fill it with information
DataRow dr = dt.NewRow();

// distinct
bool isDistinct = true;
for (int i=0; i < dt.Rows.Count; i++)
{
  // check if both rows are equal
  if (Enumerable.SequenceEqual(dt.Rows[i].ItemArray, dr.ItemArray))
  {
    // it already exists
    isDistinct = false;
    break;
  }
}

if (isDistinct)
{
  dt.Rows.Add(dr);
}
xlimit91
  • 51
  • 3
0
if ( Datatable1.Rows[NumOfRow].ToString().Deleted == "Deleted")
Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

You should check row existence by comparing primary keys:

static bool RowExists(DataTable table, DataRow row)
{
    var pk = table.PrimaryKey
                .Select(column => row[column, DataRowVersion.Original])
                .ToArray();

    return table.Rows.Contains(pk);
}

Reason is, DataRow that you are trying to check against existing DataTable is, in real-life scenarios, different class instance compared to the DataRaw in the table, even when same DataRaw already exists in the DataTable. Usual .NET equality-comparison does not work properly in this scenarios. That includes DataTable.Contains(...) method.

To properly check for DataRaw existence in the table, primary key given DataRaw should be searched for in the table.

Nenad
  • 24,809
  • 11
  • 75
  • 93
0

You can check using any with the key value

If (value.Tables(0).AsEnumerable().Any(Function(x) key = x.Field(Of Integer)("ProductId") ))

dead_webdev
  • 164
  • 2
  • 9