2012-01-25 49 views
5

我DataTable中有以下的列:刪除行,其中第一列不包含(某些字符串)

ClientID date numberOfTransactions price 

客戶端ID爲字符串類型,我需要確保其內容包括「A-」和「N6」表中的每個值。

我需要刪除DataTable中第一列(ClientID)不包含「A-」「N6」(某些總數和其他不必要的數據)的所有行。我如何從DataTable中選擇和刪除這些行?

我知道這一點:

foreach (DataRow row in table.Rows) // Loop over the rows. 
    { 

     //Here should come part "if first column contains mentioned values 
    } 

我也知道這

If (string.Contains("A-") == true && string.Contains("N6") == true) 

{ 
//Do something 
} 

我需要幫助如何實現此的每一行的第一列。

+1

你試過什麼查詢..?還有你使用SQL Server,MYSQL等DataBase ...? – MethodMan

+0

這個問題太模糊了;請給我們更多詳細信息,請 –

+1

我相信OP只是想從DataTable.Rows集合中刪除某些DataRows(在內存中),所以知道數據來自哪裏可能實際上是無關的(因此他/她的困惑)。 –

回答

6

試試這個:

編輯:完全搞砸了最後一行,所以如果你嘗試了,現在就試試,我把它並不愚蠢。 =)

List<int> IndicesToRemove = new List<int>(); 
DataTable table = new DataTable(); //Obviously, your table will already exist at this point 
foreach (DataRow row in table.Rows) 
{ 
    if (!(row["ClientID"].ToString().Contains("A-") && row["ClientID"].ToString().Contains("N6"))) 
     IndicesToRemove.Add(table.Rows.IndexOf(row)); 
} 
IndicesToRemove.Sort(); 
for (int i = IndicesToRemove.Count - 1; i >= 0; i--) table.Rows.RemoveAt(IndicesToRemove[i]); 
+0

非常好!非常感謝你! –

2

嘗試使用此,

作爲第一個柱(因此使用ItemArray [0])

for(int i=0; i<dt.Rows.Count; i++) 
{ 
    temp = dt.Rows[i].ItemArray[0].ToString(); 

if (System.Text.RegularExpressions.Regex.IsMatch(temp, "A-", System.Text.RegularExpressions.RegexOptions.IgnoreCase) || System.Text.RegularExpressions.Regex.IsMatch(temp, "N6", System.Text.RegularExpressions.RegexOptions.IgnoreCase)) 
    { 
    dt.Rows.RemoveAt(i); 
    i--; 
    } 
} 

簡單和直接的解決方案假設DT作爲Datatabe對象和客戶端ID。 ..希望它可以幫助

+1

如果你這樣做,你在迭代期間不要冒險跳過行?比方說,我= 2,你會發現需要刪除的行。你刪除行(2),調整所有其他行的索引 - 但現在我= 3,所以**新**(2)沒有得到評估,對不對? –

+0

同意,在迭代時從集合中移除將導致問題。 – Chris

+0

@ C.Barlow,你是對的。我的錯。忘了再添加一行:「我 - 」;而已。 – Bravo

2

這應該是更有效率,無論是在代碼和時間的行,請嘗試此:)

for(int x=0; x<table.Rows.Count;) 
{ 
    if (!table.Rows[x].ItemArray[0].contains("A-") && !table.Rows[x].ItemArray[0].contains("N6")) 
     table.Rows.RemoveAt(x); 
    else x++; 
} 

編碼快樂

1

前言:C.Barlow's existing answer是真棒,這僅僅是另一條路線的人可以採取。

這是爲了做到這一點,你從來沒有遍歷原始表一路(通過採取DataTable.Select()方法的優點)的一種方法:

DataTable table = new DataTable(); // This would be your existing DataTable 
// Grab only the rows that meet your criteria using the .Select() method 
DataRow[] newRows = table.Select("ClientID LIKE '%A-%' AND ClientID LIKE '%N6%'"); 
// Create a new table with the same schema as your existing one. 
DataTable newTable = table.Clone(); 
foreach (DataRow r in newRows) 
{ 
    // Dump the selected rows into the table. 
    newTable.LoadDataRow(r.ItemArray, true); 
} 

現在你有一個DataTable只你想要的行。如果必要的話,在這一點上,你可以清除原始表,並用新的內容替換它:

table.Clear(); 
table = newTable.Copy(); 


編輯:我想到了內存優化昨晚,你可以只覆蓋一旦你有你需要的行,這就避免了臨時表的需要。

DataTable table = new DataTable(); // This would be your existing DataTable 
// Grab only the rows that meet your criteria using the .Select() method 
DataRow[] newRows = table.Select("ClientID LIKE '%A-%' AND ClientID LIKE '%N6%'"); 
// Clear out the old table 
table.Clear(); 
foreach (DataRow r in newRows) 
{ 
    // Dump the selected rows into the table. 
    table.LoadDataRow(r.ItemArray, true); 
} 
+1

好! +1提高效率,一般很時髦! –

+0

@ C.Barlow哈哈,爲什麼謝謝你=) – jadarnel27

相關問題