I have an array List<SomeObject>
which contain ascending order of dates (and some more fields with info). The list will be Grouped into a List<List<SomeObject>>
, which correlate to my subject.
One List<SomeObject>
groups into an List<List<SomeObject>>
. In this former case only contain 1 row.
Some calculations occurs on LastOrDefault and FirstOrDefault of that row. (In the end, the method return one SomeObject with a date, type and calculated value).
List<List<SomeObject>> Array; // Should be filled with the grouped List<SomeObject>
List<SomeObject> // One SomeObject is a row below,
2011-01-01 enumType.Ok 140.0
2011-01-02 enumType.Ok 210.0
2011-01-03 enumType.Ok 250.0
2011-01-04 enumType.Blocking 310.0
2011-01-05 enumType.Ok 4.0
2011-01-06 enumType.Ok 34.0
2011-01-07 enumType.Ok 93.0
and counting..
Each row in the array have a type (enum) which normally are "Ok".
This array can have occurences of "NotOk" instead of "Ok" (simplified description).
So, the question is,
The row 2011-01-05 have NotOk
, which must split the array. The result will be as below. All rows up to the Blocking in one array, then next array for the rest of the rows (or to next point of "blocking".
List<SomeObject>
2011-01-01 enumType.Ok 140.0
2011-01-02 enumType.Ok 210.0
2011-01-03 enumType.Ok 250.0
2011-01-04 enumType.Blocking 310.0
List<SomeObject>
2011-01-05 enumType.Ok 4.0
2011-01-06 enumType.Ok 34.0
2011-01-07 enumType.Ok 93.0
and counting..
Then I can do a separate groupby/calculate sum on each list and lastly sum each groupby together.
[Edit]
I was helped by the answer from BrokenGlass, which worked exactly as stated. Though, I did a typo in my question that change the behavior of his answer. The first item in next array should be AFTER .Blocking. Just take his currentList.Add(item);
before the if(currentList.Count
- clause and the answer are still correct. Please also mind that this extension should be done only if needed, because we're miss the point of my Q every time it need looping each value because of "Blocking".
>`? Your code shows the latter, but your description indicates the former.