The set has been modified. enumeration may not be performed.

Source: Internet
Author: User

There is a paragraphCodeAs follows:

Code

Foreach (Campaign Campaign In Dgcampaign. selecteditems)
{
If (Campaign ! =   Null   &&   ! Resource. isnullorempty (Campaign ))
{
// Remove from itemsource in the DataGrid
Dgcampaign. itemssourcelist. Remove (Campaign );
SC. Delete (Campaign );
I ++ ;
}
}

When you use the foreach statement to perform the delete operation, the set is modified, and enumeration may not be performed.

The for statement is fine, as shown below:

Code
// This can be done as follows, but because my DataGrid has other operations, you can only use other methods
For ( Int J =   0 ; J < Dgcampaign. selecteditems. Count; j ++ )
{
Campaign Campaign = Dgcampaign. selecteditems [J] As Campaign;
Dgcampaign. itemssourcelist. Remove (Campaign );
SC. Delete (Campaign );
}

// The following method is required
Resset < Campaign > Campaigns =   New Resset < Campaign > ();
Foreach (Campaign Campaign In Dgcampaign. selecteditems)
{
If (Campaign ! =   Null   &&   ! Resource. isnullorempty (Campaign ))
{
Campaigns. Add (Campaign );
}
}
Foreach (Campaign item In Campaigns)
{
Dgcampaign. itemssourcelist. Remove (item );
SC. Delete (item );
}

 

Note:

The foreach statement implements system. collections. ienumerable or system. collections. generic. each element in an array or object set of the ienumerable <(of <(T>) interface repeats a set of embedded statements. The foreach statement is used to access the set cyclically to obtain the information you need, but cannot be used to add or remove items in the source set. Otherwise, unpredictable side effects may occur. To add or remove items in the source set, use the for loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.