-1

I want to find the elements which have the OverSized value == true and sum the BoxQuantity value of them. So far I can find the first element that has the OverSized value = true and returns the BoxQuantity value of it with the code below. Is there anyway to find all the elements that matches that condition and return sum of the box quantity values? (NOT THE SAME AS SUM AMOUNT QUESTION)

var boxNumBig = shippingSummary.ShippingBoxes.Find(item => item.OverSized == true);
int numofBigBox = boxNumBig.BoxQuantity;
Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24

2 Answers2

1

Sure, you can:

var summation = shippingSummary.ShippingBoxes
                               .Where(item => item.OverSized)
                               .Sum(x => x.BoxQuantity);

This uses the Where extension method to retain all the objects where the OverSized is true and then uses the Sum method to take the summation.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

.Find is not part of LINQ. It returns the first item in the list that it matches. I think you want to use .Where to find all of them.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445