Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

remove object from a collection of objects

Posted on 2014-12-16
4
137 Views
Last Modified: 2014-12-16
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

0
Comment
Question by:Big Monty
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40503302
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.
0
 
LVL 33

Author Comment

by:Big Monty
ID: 40503312
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
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 500 total points
ID: 40503337
Seems like "remove" usually wants the object while "removeat" has an index for the argument.  Is "RemoveAt" an option for order.Items?
0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 40503443
that was it, thx!
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question