C# DataTable,如何有条件地删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1591771/
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
DataTable, How to conditionally delete rows
提问by Alexandre Bell
I'm engaged in a C# learning process and it is going well so far. I however just now hit my first "say what?" moment.
我正在参与 C# 学习过程,到目前为止进展顺利。然而我刚刚打了我的第一个“说什么?” 片刻。
The DataTable offers random row access to its Rows collection, not only through typical collections behavior, but also through DataTable.Select. However I cannot seem to be able to tie this ability to DataRow.Delete. So far this is what it seems I need to do in order to conditionally delete one or more rows from a table.
DataTable 提供对其 Rows 集合的随机行访问,不仅通过典型的集合行为,而且通过 DataTable.Select。但是,我似乎无法将此功能与 DataRow.Delete 联系起来。到目前为止,这似乎是我需要做的,以便有条件地从表中删除一行或多行。
int max = someDataTable.Rows.Count - 1;
for(int i = max; i >= 0; --i)
{
if((int)someDataTable.Rows[i].ItemArray[0] == someValue)
{
someDataTable.Rows[i].BeginEdit();
someDataTable.Rows[i].Delete();
}
else
break;
}
someDataTable.AcceptChanges();
But I'm not happy with this code. Neither I'm convinced. I must be missing something. Am I really forced to hit the Rows collection sequentially if I need to delete one or more rows conditionally?
但我对这段代码不满意。我也不相信。我肯定错过了什么。如果我需要有条件地删除一行或多行,我真的被迫按顺序访问 Rows 集合吗?
(don't mind the inverted for. I'm deleting from the end of the datatable. So it's ok)
(不要介意倒置。我从数据表的末尾删除。所以没关系)
采纳答案by Matthew Whited
You could query the dataset and then loop the selected rows to set them as delete.
您可以查询数据集,然后循环选定的行以将它们设置为删除。
var rows = dt.Select("col1 > 5");
foreach (var row in rows)
row.Delete();
... and you could also create some extension methods to make it easier ...
...您还可以创建一些扩展方法以使其更容易...
myTable.Delete("col1 > 5");
public static DataTable Delete(this DataTable table, string filter)
{
table.Select(filter).Delete();
return table;
}
public static void Delete(this IEnumerable<DataRow> rows)
{
foreach (var row in rows)
row.Delete();
}
回答by itsmatt
I don't have a windows box handy to try this but I think you can use a DataView and do something like so:
我没有方便的 Windows 框来尝试此操作,但我认为您可以使用 DataView 并执行以下操作:
DataView view = new DataView(ds.Tables["MyTable"]);
view.RowFilter = "MyValue = 42"; // MyValue here is a column name
// Delete these rows.
foreach (DataRowView row in view)
{
row.Delete();
}
I haven't tested this, though. You might give it a try.
不过我还没有测试过这个。你可以试一试。
回答by Alain
Here's a one-liner using LINQ and avoiding any run-time evaluation of select strings:
这是使用 LINQ 并避免对选择字符串进行任何运行时评估的单行代码:
someDataTable.Rows.Cast<DataRow>().Where(
r => r.ItemArray[0] == someValue).ToList().ForEach(r => r.Delete());
回答by Sebastian Widz
Extension method based on Linq
基于Linq的扩展方法
public static void DeleteRows(this DataTable dt, Func<DataRow, bool> predicate)
{
foreach (var row in dt.Rows.Cast<DataRow>().Where(predicate).ToList())
row.Delete();
}
Then use:
然后使用:
DataTable dt = GetSomeData();
dt.DeleteRows(r => r.Field<double>("Amount") > 123.12 && r.Field<string>("ABC") == "XYZ");