I have an Object named Item:
public class Item
{
public Guid Id { get; set; }
public long Size { get; set; }
}
in a List<Item>
What I need is to query this list and return a number of rows where the
cumulated size (Size property of Item) does not exceed a certain number (threshold).
I could then process these elements, remove them from the original list and get the next
elements with the same query until all elements have been processed.
Example List:
Id Size
----------------
1 10
2 13
3 5
4 30
5 10
Say my desired threshold is a maximum size of 25.
The query should return the items with Id 1 & 2 since their cumulated size doesn’t exceed
25. Then I remove these two values. The next time the query should return 3 & 5.
Id 4 should never be returned since it exceeds 25.
Any ideas?
I think you need to store some running sum value, but
TakeWhileshould do the job.Something like this: