I want to get a subset of objects from a List based on the value of one of the object properties, specifically I want to get the first few objects based on the sum of their aggregated value of that property.
I could manually iterate through the list, add/Sum the value of the property and compare the result to my desired value, but is there a better way?
For example, imagine I have this list:
List<MyObj> MyObjList;
Where MyObj looks like this:
public class MyObj
{
public int MyValue { get; set; }
}
And MyObjList has the following objects and values, in this order:
MyObjList[0].MyValue = 1;
MyObjList[1].MyValue = 3;
MyObjList[2].MyValue = 2;
MyObjList[3].MyValue = 3;
MyObjList[4].MyValue = 2;
For example I might want to get the first few items where their collective sum of MyValue <= 5, which would return only the first 2 objects.
How would you do that?