-5

I know this is a dumb question because you cannot modify the loop collection while in a loop, but I do need to change it. I know I must not change the referenced objects, but I have no idea how to do this.

var orders = _orderService.GetOrders(o => !o.Deleted &&
                                          o.OrderStatus != OrderStatus.Cancelled &&
                                          o.OrderStatus != OrderStatus.Complete);
foreach (var order in orders)
{
    if (order.PaymentStatus == PaymentStatus.Paid)
    {
        if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
        {       
           var tempOrder = _orderService.GetOrderById(order.Id);                 
            SetOrderStatus(tempOrder , OrderStatus.Complete, true);
        }
    }
}

I always get an error.

UPDATED: I changed to this

 var orders = _orderService.GetOrders(o => !o.Deleted &&
                         o.OrderStatus != OrderStatus.Cancelled && o.OrderStatus != OrderStatus.CompletE);

            List<int> orderIndex = new List<int>();
            orders.ToList().ForEach(x => orderIndex.Add(x.Id));

           foreach(var index in orderIndex)
           {
                var order = _orderService.GetOrderById(index);
                if (order.PaymentStatus == PaymentStatus.Paid)
                {
                    if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
                    {

                        SetOrderStatus(order, OrderStatus.Complete, true);
                    }
                }
            }
nam vo
  • 3,271
  • 12
  • 49
  • 76
  • 3
    Which error do you get? – John Saunders Oct 04 '13 at 11:59
  • `because you cannot change value while in a loop` .. Erm, what? – walther Oct 04 '13 at 12:05
  • @walther I think he's referring to the [`"Collection was modified; enumeration operation may not execute"`](http://stackoverflow.com/q/3551696/2246344) error. – Sam Oct 04 '13 at 12:12
  • @Sam, I know, but it has nothing to do with modifying collection items... – walther Oct 04 '13 at 12:14
  • @Sam: that is what he thinks his problem is, but that obviously can't happen from the posted code. I want him to post the actual exception. – John Saunders Oct 04 '13 at 12:14
  • I know, I can see he's only modifying `tempOrder`, I was just clarifying what the OP was saying. – Sam Oct 04 '13 at 12:21
  • @Sam: we don't actually know what the OP is saying. He could be quoting something he heard from a colleague. – John Saunders Oct 04 '13 at 12:53
  • @nam vo, even if you solved your problem, don't leave the post unuseful to others - post the exception you get. – Yosi Dahari Oct 04 '13 at 12:58
  • What I mean is; I was just rephrasing what he said. I was just saying that the error I quoted Is most likely what he was trying to quote. – Sam Oct 04 '13 at 12:59

2 Answers2

3

try

int count = orders.Count;  // the length of the collect : may need a different method for different collection types.
for(int i = 0; i < count; i++)
{
    var current = orders[i];
    // do stuff with current.
}
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • Your answer seems to assume that his problem is "can't change the range object during the iteration". But if he's not changing it, then that's not the problem, and your answer is not the solution. – John Saunders Oct 04 '13 at 12:06
  • But that call doesn't use `order`. Using `tempOrder` wouldn't change `order` – John Saunders Oct 04 '13 at 12:07
  • @JohnSaunders - How do you know that it won't change order? GetOrderById is custom code which may (or may not) return the same reference in the loop. – Yosi Dahari Oct 04 '13 at 12:21
  • @Yosi: It's not a question of returning the same reference - it's a question of changing the range variable, `order`. – John Saunders Oct 04 '13 at 12:52
0

use for loop instead of foreach loop

for(int i=0; i<orders.Count; i++)
{
    if (orders[i].PaymentStatus == PaymentStatus.Paid)
    {
        if (orders[i].ShippingStatus == ShippingStatus.ShippingNotRequired || orders[i].ShippingStatus == ShippingStatus.Delivered)
        {       
           var tempOrder = _orderService.GetOrderById(orders[i].Id);                 
            SetOrderStatus(tempOrder , OrderStatus.Complete, true);
        }
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • @Yosi now it's edited, so I removed my post, but even if answer can be short I think that writing something more is a good idea. Answer like: "write loop" is not a good idea – Kamil Budziewski Oct 04 '13 at 12:02
  • @JohnSaunders - You don't have to write the code for whom asked the question, an answer, solves the issue, even by words, that prevented the code from doing what it should. – Yosi Dahari Oct 04 '13 at 12:04
  • 1
    @Yosi: what makes you think that this is what was preventing the code from doing what it should? The OP never told us what error he received. – John Saunders Oct 04 '13 at 12:08
  • @JohnSaunders - "you cannot change value while in a loop, but i do need to change it" - not the perfect phrasing, but you can understand what he means. – Yosi Dahari Oct 04 '13 at 12:10
  • 1
    @Yosi: I understand what he thinks his problem is, and I understand that he's obviously wrong about that. He needs to post the actual exception. – John Saunders Oct 04 '13 at 12:13
  • IMHO, you shouldn't really be posting an answer when you're not 100% sure what the problem is. The OP is obviously confused, and hasn't added any more details about the exception yet, so the problem could be anything. Of course it's nice to help out, but still, trying to answer a detail-less post can confuse the OP even more. – Sam Oct 04 '13 at 12:28
  • @Yosi, of course it SHOULD be downvoted, because it tries to solve a completely different problem! – walther Oct 07 '13 at 10:30