web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Early bound plugin, can't use loadProperty

(0) ShareShare
ReportReport
Posted on by

The following code snippet is throwing a "Collection was modified; enumeration operation may not execute", specifically on the loadProperty. I can't quite figure out why, and all my workarounds haven't worked out for me. Any suggestions? I basically just want to load in all sales order details on the order.

public void ExecutePluginLogic(IServiceProvider serviceProvider)
        {
            // Use a 'using' statement to dispose of the service context properly
            // To use a specific early bound entity replace the 'Entity' below with the appropriate class type
            using (var localContext = new LocalPluginContext<Entity>(serviceProvider))
            {
                var activeOrders = (
                    from o in localContext.CrmContext.CreateQuery<SalesOrder>()
                    where o.StateCode.Equals(SalesOrderState.Active)
                    select o).Take(5).ToList();

                foreach (var order in activeOrders)
                {
                    localContext.CrmContext.LoadProperty(order, nameof(SalesOrder.order_details));
                }
            }
        }


*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Early bound plugin, can't use loadProperty

    Hi Dnar,

    I recently have the same issure you have here. Could you share your finding if there's any?

    Thanks in advance!

    Regards,

    Junjie

  • Suggested answer
    Michel van den Brink Profile Picture
    on at
    RE: Early bound plugin, can't use loadProperty

    Hello Dnar,

    The issue you are experiencing has to do with the way collections can be enumerated with an IEnumerable implementation. An implementation of IEnumerable makes a class compatible with foreach, such as List<T> and arrays.

    There is one quirk to these IEnumerable classes, while you are looping through an instance of them, you are not allowed to change the instance you are looping though. This is a fail safe mechanism to make sure the foreach-loop doesn't lose its place in the collection, as you are changing the collection you are looping though you might have also added/removed items, which might break the loop.

    .

    You can remedy this by using a regular for-loop instead

    public void ExecutePluginLogic(IServiceProvider serviceProvider)
            {
                // Use a 'using' statement to dispose of the service context properly
                // To use a specific early bound entity replace the 'Entity' below with the appropriate class type
                using (var localContext = new LocalPluginContext<Entity>(serviceProvider))
                {
                    var activeOrders = (
                        from o in localContext.CrmContext.CreateQuery<SalesOrder>()
                        where o.StateCode.Equals(SalesOrderState.Active)
                        select o).Take(5).ToList();
    
                    for (int i = 0; i < activeOrders.Count; i++)
                    {
                        localContext.CrmContext.LoadProperty(activeOrders[i], nameof(SalesOrder.order_details));
                    }
                }
            }


    Hope this helps you out, let me know if the code doesn't work for you or if you have additional questions.

    If you found this answer helpful, please help the community by marking it as verified :-)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans