Given the following object:
public class Product {
string Name {get;}
int Quantity {get;}
}
using Linq, how would I query a Li
Here is a quick 2 liner.
var sum = 0;
var query = col.Where(x => { var temp = sum; sum += x.Quantity; return temp < 500; });
Replace 500 with the constant or variable of your choosing.
EDIT
Here is mquander's more efficient solution
var sum = 0;
var query = col.TakeWhile(x => { var temp = sum; sum += x.Quantity; return temp < 500; });
You just create a variable to hold the sum, then add to it as each item in the list is tested with the where
clause:
int sum = 0;
from product in list where (sum += product.Quantity) < 8 select product