C# 循环遍历数据表并删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through a datatable and remove row
提问by
I have populated a Datatable, from 2 different servers. I am able to make adjustments where my length>0, what I want to do is remove the rows that does not hit. Here is a summary of what I have
我从 2 个不同的服务器填充了一个数据表。我可以在我的长度> 0 的地方进行调整,我想要做的是删除没有命中的行。这是我所拥有的摘要
DataRow[] dr = payments.dtPayments.Select(myselect);
if (dr.Length > 0)
{
for (int a = 0; a < dr.Length; a++)
{
if (thisOption == true)
dr[0].Delete();
else if (otherOption == true)
{
dr[0]["Date"] = myDataReader["date"].ToString().Trim();
dr[0]["Pay"] = payTypeName(myDataReader["ccdsrc"].ToString()
.Trim());
}
}
}
if (dr.Length == 0)
{
if (LastOption == true)
{
//DataRow should be removed
}
}
采纳答案by tvanfosson
If dr.Length is zero, then your select did not return any rows. If you want to delete rows that don't match your criteria, then I would implement that as a different type of query. It could also be that I am completely misunderstanding your question. Could you update your question to show your SQL and indicate where in the code sample you are having the problem: inside the loop or after the loop where you have your comment?
如果 dr.Length 为零,则您的选择没有返回任何行。如果您想删除与您的条件不匹配的行,那么我会将其实现为不同类型的查询。也可能是我完全误解了你的问题。您能否更新您的问题以显示您的 SQL 并指出您在代码示例中遇到问题的位置:在循环内部还是在您发表评论的循环之后?
回答by Sergio
Have you tryed this?
你试过这个吗?
payments.dtPayments.Rows.Remove(dr)
Payments.dtPayments.Rows.Remove(dr)
回答by Yona
I'm not sure I completely understand your question but it seems that you try to remove an entry from the collection while you are still looping over it. (which will cause an array index error)
我不确定我是否完全理解你的问题,但你似乎试图在你仍在循环的时候从集合中删除一个条目。(这会导致数组索引错误)
You should save a reference to each entry you want to delete in a new collection and then remove all the new entries from the old collection:
您应该在新集合中保存对要删除的每个条目的引用,然后从旧集合中删除所有新条目:
DataRow[] dr = payments.dtPayments.Select(myselect);
List<DataRow> rowsToRemove = new List<DataRow>();
for (int a = 0; a < dr.Length; a++) {
if(/* You want to delete this row */) {
rowsToRemove.Add(dr[a]);
}
}
foreach(var dr in rowsToRemove) {
payments.dtPayments.Rows.Remove(dr);
}
回答by Chris Lees
You need to create a datarow outside the loop, then when you get to the row you want to remove assign it to the row you created outside. and break the loop. Then below the loop you can remove it.
您需要在循环外创建一个数据行,然后当您到达要删除的行时,将其分配给您在外部创建的行。并打破循环。然后在循环下方,您可以将其删除。
DataRow r = null;
foreach(DataRow row in table.Rows)
{
if(condition==true)
{
r = row;
break;
}
}
table.Rows.Remove(r);
回答by Rick Strahl
You can loop through the collection in reverse (for (int i=rows.length-1;x>-1;x--) ) to remove multiple rows to ensure you won't get index out of bound errors.
您可以反向遍历集合(for (int i=rows.length-1;x>-1;x--))以删除多行,以确保不会出现索引越界错误。