Big Monty
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
}
}
{
}
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that was it, thx!
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.