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

Recommended Answers

All 3 Replies

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

But i want to delete the data from both the collections

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.