Adding entities to collection

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 10-Feb-2005 21:12:05   

I'm trying to add entities of one collection into the collection of an existing entity sub collection. What I have is a collection of purchase order items that have been created and added to a purchase order. I know want to take this collection if po items and add them to the collection of a purchase order that exists in the db and has other po items already added to it. So what I would like to happen is that the new po items get added onto the end of the existing po items.

What happens now is that when this code executes I get this error:


Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Source Error: 

Line 85:            MyPurchaseOrderEntity order = new MyPurchaseOrderEntity(purchaseOrderID);
Line 86:            order.IsNew = false;
Line 87:            foreach(MyPurchaseOrderItemEntity currentItem in this.order.PurchaseOrderItem)
Line 88:            {
Line 89:                order.PurchaseOrderItem.Add(currentItem);

Is this possible?


            int purchaseOrderID = Convert.ToInt32(this.openOrders.SelectedValue);
            // Save order items into selected po.
            MyPurchaseOrderEntity order = new MyPurchaseOrderEntity(purchaseOrderID);
            order.IsNew = false;
            foreach(MyPurchaseOrderItemEntity currentItem in this.order.PurchaseOrderItem)
            {
                order.PurchaseOrderItem.Add(currentItem);
            }
            ServiceFactory.GetPersistanceManager().SaveEntity( order );

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Feb-2005 22:16:55   

You can't add to a collection that you're also emumerating over. you use this.order but that's of course the order object you just created in the line above that foreach line. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 10-Feb-2005 23:26:20   

Otis wrote:

You can't add to a collection that you're also emumerating over. you use this.order but that's of course the order object you just created in the line above that foreach line. simple_smile

I'm not sure what I'm doing wrong. In the code below I have this.order which is a page level instance variable containing a PurchaseOrderEntity instance and its PurchaseOrderItemEntity collection. I then create a different instance of PurchaseOrderEntity and pass it a primary key value to give it a reference to its database record. I now want to copy the PurchaseOrderItem Entities from the page level instance of PurchaseOrderEntity to the method level instance of the PurchaseOrderEntity.PurchaseOrderItem collection.

The page level instance of the PurchaseOrderEntity has a collection of PurchaseOrderItem entities, but they don't exist in the database yet. These PurchaseOrderItem entities I have created from data in another table and now want to add to the PurchaseOrder table. So what I am trying to do is copy the PurchaseOrder entities into the PurchaseOrderItem collection of the PurchaseOrder that does exist in the database.

I hope I'm expaining this ok. It's not easy trying to figure out how to write this. I'll trying writing another example of code that I think will make this work, but its a more messy way of writing the code.


        private void addToPO_Click(object sender, EventArgs e)
        {
            int purchaseOrderID = Convert.ToInt32(this.openOrders.SelectedValue);
            // Save order items into selected po.
            MyPurchaseOrderEntity purchaseOrder = new MyPurchaseOrderEntity(purchaseOrderID);
            order.IsNew = false;
            foreach(MyPurchaseOrderItemEntity currentItem in this.order.PurchaseOrderItem)
            {
                purchaseOrder.PurchaseOrderItem.Add(currentItem);
            }
            //ServiceFactory.GetPersistanceManager().SaveEntity( order );

        }

My goal is to take a collection of PurchaseOrderItem entities that don't exist in the PurchaseOrderItem table and add them to a collection of PurchaseOrderItem entities that do exist in the PurchaseOrderItem table therby creating a larger collection of in the PurchaseOrderItem table.

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 10-Feb-2005 23:58:52   

Well, I just tried the code that I thought would work and it did. Now if I could only do this using the previously posted code I would have eight fewer lines of code and a lot less typing.


        private void addToPO_Click(object sender, EventArgs e)
        {
            int purchaseOrderID = Convert.ToInt32(this.openOrders.SelectedValue);
            // Save order items into selected po.
            MyPurchaseOrderEntity purchaseOrder = new MyPurchaseOrderEntity(purchaseOrderID);
            order.IsNew = false;
            foreach(MyPurchaseOrderItemEntity currentItem in this.order.PurchaseOrderItem)
            {
                MyPurchaseOrderItemEntity newItem = new MyPurchaseOrderItemEntity();
                newItem.PurchaseOrderID = currentItem.PurchaseOrderID;
                newItem.BuildID = currentItem.BuildID;
                newItem.ProductVersionID = currentItem.ProductVersionID;
                newItem.ItemDescription = currentItem.ItemDescription;
                newItem.QuantityOrdered = currentItem.QuantityOrdered;
                newItem.UnitCost = currentItem.UnitCost;
                newItem.ExtCost = currentItem.ExtCost;
                purchaseOrder.PurchaseOrderItem.Add(newItem);
            }
            ServiceFactory.GetPersistanceManager().SaveEntity( purchaseOrder );
        }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Feb-2005 09:33:38   

tprohas wrote:

Otis wrote:

You can't add to a collection that you're also emumerating over. you use this.order but that's of course the order object you just created in the line above that foreach line. simple_smile

I'm not sure what I'm doing wrong. In the code below I have this.order which is a page level instance variable containing a PurchaseOrderEntity instance and its PurchaseOrderItemEntity collection. I then create a different instance of PurchaseOrderEntity and pass it a primary key value to give it a reference to its database record. I now want to copy the PurchaseOrderItem Entities from the page level instance of PurchaseOrderEntity to the method level instance of the PurchaseOrderEntity.PurchaseOrderItem collection.

What you do wrong is that you declare a new 'order' object right above the foreach. Now, in the foreach, you both traverse over this object's collection (this.order. ..) and add to it. That doesn't make sense.

The mistake is that you shouldn't name your new object 'order' as it will hide a higher scoped object. Same odd things happen if you declare a private class member _foo and in a forloop you declare again a _foo. simple_smile

[quotenick="tprohas"]Well, I just tried the code that I thought would work and it did. Now if I could only do this using the previously posted code I would have eight fewer lines of code and a lot less typing.

this code should work too:


private void addToPO_Click(object sender, EventArgs e)
{
    int purchaseOrderID = Convert.ToInt32(this.openOrders.SelectedValue);
    // Save order items into selected po.
    MyPurchaseOrderEntity purchaseOrder = new MyPurchaseOrderEntity(purchaseOrderID);
    order.IsNew = false;
    foreach(MyPurchaseOrderItemEntity currentItem in this.order.PurchaseOrderItem)
    {
        purchaseOrder.PurchaseOrderItem.Add(currentItem);
    }
    ServiceFactory.GetPersistanceManager().SaveEntity( purchaseOrder );
}

Here you traverse over collection this.order.PurchaseOrderItem, and add to purchaseOrder.PurchaseOrderItem, i.e. you aren't traversing the same collection as you're adding to

Frans Bouma | Lead developer LLBLGen Pro