Given the following object:
public class Product {
string Name {get;}
int Quantity {get;}
}
using Linq, how would I query a List<Product> until I got a sum >= a given quantity? In other words, if my list looked like
Name Quantity
-----------------
prod1 5
prod2 6
prod7 7
I want to query the List and pull instances until I get a Sum >=8. In this case I’d get the first two items in the List. If I wanted sum >= 12, I’d get all three.
I know I can write a loop to do this for me, but I was fantasizing that there was some slick one-liner using Linq to achieve the same thing.
Thanks
Here is a quick 2 liner.
Replace 500 with the constant or variable of your choosing.
EDIT
Here is mquander’s more efficient solution