0

I have two collection(array list)

foreach (string templine1 in arrline1)//arrline1 is the first collection
{
        int counti = 0
      foreach (string templine1 in arrline1)//arrline2 is the second collection
      {
        int countj = 0

      }
      arrline1.RemoveAt(countj);

      arrline1.RemoveAt(counti);
}

But giving error [B]Cannot Modify Collection[/B]

I am spliting the line from two collection
And in inner foreach loop i am checking the two columns of both line
If equal doing some processing and writing to another file
and i want to delete this both line
As Both the file may contain more data and to avoid the slow processing
I want to delete the two lines from the collection

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Saikalyankumar
1

When using an enumerator to iterate through a collection (which is what foreach is doing) you can't modify the collection as that invalidates the enumerator.

You'll need to create a third list of items to delete and delete them once you are done with your foreach loops

0

Ya, better its the idea given by @Momerath is Correct, u can't loop through a Array Collection or a Data View by editing it inside. So, first u assign the Arraylist to the Third list and later compare the counti and countj in that two array lists and delete.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.