Checking value in column foreach row

2019-09-03 02:37发布

The DataTable (dt) stores the retrieved values of carID's and makes so it stores like 3 rows within the DataTable, I also have another DataTable called dt2 which also stores carID's and makes, I am trying to loop through each row in the dt to see if any carID stored in the dt exists in any of the rows in dt2, here is what I have so far:

DataTable dt = w.getUserCars(userID);

foreach (DataRow dr in dt.Rows)
{
  string carID = dr["carID"].ToString();

}

How do I do this?

1条回答
在下西门庆
2楼-- · 2019-09-03 03:14

You should be able to achieve this using DataTable.Select() method. You are on the right track. You just need to add the method to find the Row(s) in dt2.

DataTable dt = w.getUserCars(userID);
DataRow[] foundRows;

foreach (DataRow dr in dt.Rows)
{
  string carID = dr["carID"].ToString();

  foundRows = dt2.Select("carID = " + carID);
  // do stuff here with foundRows

  foreach (DataRow r in foundRows)
  {
    r.Delete();
  }
}
查看更多
登录 后发表回答