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());
                            individualItem.Remove(index);      <-- getting an error here saying no definition for Remove() exists



Open in new window

LVL 34
Big MontySenior Web Developer / CEO of Asked:
Who is Participating?
UnifiedISConnect With a Mentor Commented:
Seems like "remove" usually wants the object while "removeat" has an index for the argument.  Is "RemoveAt" an option for order.Items?
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.
Big MontySenior Web Developer / CEO of Author Commented:


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

I actually have


as the line right after removing it, so I shouldn't lose any of the objects in the collection
Big MontySenior Web Developer / CEO of Author Commented:
that was it, thx!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.