3

I have the following code. How to convert this into a LINQ? Any one please help me.

In the below code 'powd' contains the db values and purchaseOrderUpdateRequest contains data submitted by user.

bool hasUpdate;

foreach (var item in purchaseOrderUpdateRequest.Lines)
{
    var line = powd.PurchaseOrderLines.Single(p => p.ItemNumber ==  item.ItemNumber);
    decimal quantityToReceive = item.QuantityReceived - line.QuantityReceivedToDate;

    if (quantityToReceive > 0)
    {
        hasUpdate =true;
        break;
    }
}

Thanks.

0

2 Answers 2

13

So all you're trying to work out is whether any item has an update? That's just:

var hasUpdate = purchaseOrder.UpdateRequest.Lines.Any(item =>
     item.QuantityReceived < powd.PuchaseOrderLines
                                 .Single(p => p.ItemNumber == item.ItemNumber)
                                 .QuantityReceivedToDate)

I've changed the comparison to just use a direct < rather than a subtraction and a comparison with 0, but this should be okay.

Sign up to request clarification or add additional context in comments.

Comments

0

Untested, but this should do the same and might be easier to read:

   var quantities = (from item in purchaseOrderUpdateRequest.Lines
                     from item2 in powd.PurchaseOrderLines
                     where item.ItemNumber == item2.ItemNumber
                     select item.QuantityReceived - item2.QuantityReceivedToDate);
   hasUpdate = quantities.Any(quantityToReceive => (decimal) quantityToReceive > 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.