PC Review


Reply
Thread Tools Rate Thread

Delete elements in .NET collections

 
 
headware
Guest
Posts: n/a
 
      9th Aug 2005
A common issue that I see in the
microsoft.public.dotnet.framework.adonet group is that if you want to
delete a DataRow from a collection of DataRows (e.g. myTable.Rows), you
can't do this using a foreach loop like this:

foreach(DataRow row in myTbl.Rows)
if(someCondition)
row.Delete();

You must instead use a regular for loop and loop from back to front
like this:

for(int i = myTbl.Rows.Count - 1; i >= 0; i--)
if(someCondition)
myTbl.Rows[i].Delete();

I understand why you must do this. My question is whether this is a
standard situation in .NET collection classes.

I have an XML file that I'm processing using the DOM classes
(XmlDocument, XmlNode, etc.) and I need to loop through the children of
an XmlNode and call a function on each one that may or may not delete
that child. Of course, if I use a foreach loop or a for loop that loops
from front to back, the loop counter gets messed up when I delete a
node. It seems to work when I use the same solution as listed above
with the DataRows, but I wanted to confirm that it was a valid solution
in this case.

I can't find any Microsoft documentation regarding this so I was hoping
somebody would be able to point me to some or maybe there would be
somebody with some "inside" knowledge about this issue.

Thanks,
Dave

 
Reply With Quote
 
 
 
 
Lloyd Dupont
Guest
Posts: n/a
 
      10th Aug 2005
yep, it's a common situation
I find it annoying too, but makes sense....

"headware" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>A common issue that I see in the
> microsoft.public.dotnet.framework.adonet group is that if you want to
> delete a DataRow from a collection of DataRows (e.g. myTable.Rows), you
> can't do this using a foreach loop like this:
>
> foreach(DataRow row in myTbl.Rows)
> if(someCondition)
> row.Delete();
>
> You must instead use a regular for loop and loop from back to front
> like this:
>
> for(int i = myTbl.Rows.Count - 1; i >= 0; i--)
> if(someCondition)
> myTbl.Rows[i].Delete();
>
> I understand why you must do this. My question is whether this is a
> standard situation in .NET collection classes.
>
> I have an XML file that I'm processing using the DOM classes
> (XmlDocument, XmlNode, etc.) and I need to loop through the children of
> an XmlNode and call a function on each one that may or may not delete
> that child. Of course, if I use a foreach loop or a for loop that loops
> from front to back, the loop counter gets messed up when I delete a
> node. It seems to work when I use the same solution as listed above
> with the DataRows, but I wanted to confirm that it was a valid solution
> in this case.
>
> I can't find any Microsoft documentation regarding this so I was hoping
> somebody would be able to point me to some or maybe there would be
> somebody with some "inside" knowledge about this issue.
>
> Thanks,
> Dave
>



 
Reply With Quote
 
Peter Sestoft
Guest
Posts: n/a
 
      10th Aug 2005
"headware" <(E-Mail Removed)> writes:

> A common issue that I see in the
> microsoft.public.dotnet.framework.adonet group is that if you want to
> delete a DataRow from a collection of DataRows (e.g. myTable.Rows), you
> can't do this using a foreach loop like this:
>
> foreach(DataRow row in myTbl.Rows)
> if(someCondition)
> row.Delete();
>
> You must instead use a regular for loop and loop from back to front
> like this:
>
> for(int i = myTbl.Rows.Count - 1; i >= 0; i--)
> if(someCondition)
> myTbl.Rows[i].Delete();


This can be marvellously inefficient if the row set is large and more
than a few rows are to be deleted: the runtime is most likely
quadratic.

Peter
 
Reply With Quote
 
headware
Guest
Posts: n/a
 
      12th Aug 2005
Of course, if Micorsoft changes the code so that when an item is
deleted from the list, it fills in the empty space from front to back
instead of back to front, that's going to break lots of code.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete row with the same elements as the one above Arno Microsoft Excel Programming 3 24th Nov 2008 05:40 PM
delete collections? David Windows XP MovieMaker 5 7th Apr 2008 06:18 PM
Can't delete elements in folder Recent elements Gudmund Liebach Nielsen Windows Vista General Discussion 0 16th Oct 2007 04:06 PM
Is is possible to remove elements from the middle of a System.Collections.Queue? Daniel Microsoft Dot NET 2 10th Feb 2005 06:11 PM
C++ collections are bringing repeated elements to C#... why? Marcelo Microsoft C# .NET 2 16th Jan 2004 11:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:27 PM.