I have a datarow array DataRow[] drRequest
with 5 rows. Then i am iterating each row using foreach
and deleting rows using
foreach (DataRow dr in drRequest)
{
dr.Delete();
but afterwards the drRequest.Length
is returning 5 instead of 0.
I can't use Datatable
, i need to use DataRow[]
DataRow.Delete
doesn't remove this row from the DataTable
. It just marks this row as "will be deleted from the datasource if you use a DataAdapter and call dataAdapter.Update". If you want to remove it use Remove
:
foreach (DataRow dr in drRequest)
{
dr.Table.Rows.Remove(dr);
}
If drRequest
really is a DataRow[]
, then it will always have length (whatever it was to start with). Arrays never change length . Never. You can null
a value out of it, but ... that may not be expected by whatever other code is looking at the array later. This is simply a feature of what arrays are .
You might consider an alternative data structure. For example, you can remove things from a List<T>
. But note that behind the scenes this involves a shuffle-down. If you are selectively removing things, then list.RemoveAll(x => {return true to remove, false to keep});
knows how to do this optimally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.