문제

I've been working on this code for a while now, and am very slow at this point. I don't know if the answer is obvious, but I haven't been able to think of a way to convert this bit:

foreach (Item i in stockList)
{
    if (i == order.OrderItem)
        i.ChangeStock(order.NumberOfItems);
}
outStandingOrders.Remove(order);

into a lambda expression. The best I could come up with is

stockList.ForEach(i => i == order.OrderItem)

(Don't know where to go from here)

There is also only ever one item in stockList that is equal to order.OrderItem.

Any help would be hot

Thanks!

도움이 되었습니까?

해결책

Basing on your information that "There is also only ever one item in stockList that is equal to order.OrderItem", I would write it simply:

 var item = stockList.FirstOrDefault(i => i == order.OrderItem);
 if (item != null)
 {
     item.ChangeStock(order.NumberOfItems);    
 }

다른 팁

stockList.FindAll(i => i == order.OrderItem)
.ForEach(i => i.ChangeStock(order.NumberOfItems));

Untested, just typed ^^

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top