2014-10-31 107 views
0

我有兩個DataTable s。無法從DataTable中刪除基於另一個DataTable的行

distinct

+-------+-------+ 
| host | count | 
+-------+-------+ 
| comp1 |  2 | 
+-------+-------+ 
| comp2 |  3 | 
+-------+-------+ 
| comp3 |  2 | 
+-------+-------+ 

t

+----+-------+ 
| id | host | 
+----+-------+ 
| 1 | comp1 | 
+----+-------+ 
| 2 | comp2 | 
+----+-------+ 
| 3 | comp1 | 
+----+-------+ 
| 4 | comp3 | 
+----+-------+ 
| 5 | comp1 | 
+----+-------+ 
| 6 | comp3 | 
+----+-------+ 

我想從t其中包含distinct表小於3count列中的計算機名稱中刪除所有行。代碼:

for (int i = 0; i < distinct.Rows.Count; i++) 
{ 
    if (Convert.ToInt32(distinct.Rows[i][1]) < 3) 
    { 
     foreach (DataRow row in t.Select()) 
     { 
      if (row[1].ToString() == distinct.Rows[i][0].ToString()) 
      { 
       row.Delete(); 
      } 
     } 
    } 
} 

但它什麼也沒做。如果我在上面的代碼之前和之後比較t的內容,它就是一樣的。我在這裏錯過了什麼?

+1

不能刪除行這樣的。你需要'DataTable.Rows.RemoveAt(Index)'或'DataTable.Rows.Remove(Datarow)'在你的情況下用't.Rows.Remove(row);'替換'row.Delete();'' – Franck 2014-10-31 12:08:45

+0

是的,你很棒。它正在這樣工作。 – fishmong3r 2014-10-31 12:34:14

回答