tags:

views:

102

answers:

2
+1  Q: 

Foreach in Linq

Can I replace these foreach's with LINQ or delegates?

    foreach (List<Shopping.BasketPayment> payList in Basket.basketPayments)
    { 
        foreach (var pay in payList)
        {
            if (pay.paymentMethod == "Money Order" 
            {
                foreach (OrderItem orderItem in order.Items)
                {
+1  A: 
var qry = from payList in Basket.basketPayments
          from pay in payList
          where pay.paymentMethod == "Money Order"
          from orderItem in order.Items // where did "order" come from?
          ... etc ending in `select` of one or more articles

foreach(var item in qry) {
    // do something with item
}
Marc Gravell
A: 
var allPayments = Basket.basketPayments.SelectMany(p => p);
var moneyOrders = allPayments.Where(p => p.paymentMethod == "Money Order");

foreach(var payment in moneyOrders)
{
    foreach(OrderItem item in order.Items)
    {

    }
}
mquander