我有DataTable,其中包含以下列:
ClientID date numberOfTransactions price
Run Code Online (Sandbox Code Playgroud)
ClientID的类型为string,我需要确保其内容包含表中每个值的"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
}
Run Code Online (Sandbox Code Playgroud)
我也知道这一点
If (string.Contains("A-") == true && string.Contains("N6") == true)
{
//Do something
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助如何为每行的第一列实现此功能.
试试这个:
编辑:完全搞砸了最后一行,所以如果你尝试过,现在就尝试一下,我让它变得愚蠢.=)
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]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4174 次 |
| 最近记录: |