Link to home
Start Free TrialLog in
Avatar of Big Monty
Big MontyFlag for United States of America

asked on

remove object from a collection of objects

How can I remove the "individualItem" object from the pendingOrders collection using the code below?

                IEnumerable<PendingOrders> pendingOrders = dbHelper.getPendingOrders(_mode);

                Item individualItem;

                foreach (PendingOrders order in pendingOrders)
                {
                   // foreach (Item individualItem in order.Items)
                    for (int index = 0; index < order.Items.Count; index++ )
                    {
                        individualItem = new Item();
                        individualItem = order.Items[index];

                        _webServiceURL = string.Format(CCG_GETQUANTITY_URL + "{0}/?token={1}", individualItem.CCGSKU, _ccg_token.ToString());
                        List<CCG_OrderData> ccgData = getCCG_ItemData(_webServiceURL);

                        //-- if the number of items on order is less than what they have in stock, proceed
                        if (individualItem.Quantity <= ccgData[0].Balance_On_Hand)
                        {
                            individualItem.InStock = true;
                            //-- TO DO: add in address verification for US orders
                            //var status = verifyOrderAddress(order, "?token=" + _ccg_token.ToString());
                        }
                        else
                        {
                            individualItem.Remove(index);      <-- getting an error here saying no definition for Remove() exists
                        }
                    }


                    {

                    }
                }

Open in new window

Avatar of UnifiedIS
UnifiedIS

I think you want to do something like this: order.Items.remove(index)
However, removing the object may cause an error in your loop.
You might need to collect all the items that will be removed during the evaluation loop and then remove the items in a separate loop based on the items that were flagged.
Avatar of Big Monty

ASKER

using

order.Items.Remove(index)

I now get a message saying it has an invalid argument.

I actually have

index--;

as the line right after removing it, so I shouldn't lose any of the objects in the collection
ASKER CERTIFIED SOLUTION
Avatar of UnifiedIS
UnifiedIS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that was it, thx!