Checking value in column foreach row

吃可爱长大的小学妹 提交于 2019-12-25 03:37:07

问题


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:


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();
  }
}


来源:https://stackoverflow.com/questions/29950415/checking-value-in-column-foreach-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!