I need to group a list (really a datatable but for simplicity) based on 3 columns where in the first column in really a representation of a fiscal year in integer form. I need to group on all rows where:
- the current fiscal year has the second column (Service) equal to 1 and
- the following fiscal year is equal to 1, and any consecutive fiscal years have 1 as the Service
The end result group will then have merged all consecutive fiscal years that have 1 as the Service, by taking the first year the group started to the last year it ended while summing the Service and Earnings.
var list = new List(){
{20002001, 1 , 100.00},
{20012002, .5 , 100.00},
{20022003, 1.0 , 100.00},
{20042005, 1.0 , 50.00},
{20052006, 1.0 , 50.00
};
Should produce the results:
20002001, 1 , 100.00
20012002, 1 , 100.00
20022006, 3 , 200.00
This is not a legible example to work off of but maybe it will shed light on what I am attempting to work towards:
var test = from r in list
where r.Item("FiscalYear") + 10001 = list.SkipWhile(r2 => !r.Equals(current)).Skip(1).FirstOrDefault(r3 => r3.Item("FiscalYear")) &&
r.Item("Service") = 1 &&
list.SkipWhile(r2 => !r.Equals(current)).Skip(1).FirstOrDefault(r => r.Item("Service") = 1D)
Select New { FiscalYear = $"{r.Item("FiscalYear") % 1000}{I have NO IDEA HOW TO DETERMINE THIS PART}", Service = list.Sum(r => r.Item("FiscalYear"), Earnings = list.Sum(r => r.Item("Earnings"))
Assuming the get next would work from http://www.herlitz.nu/2011/12/01/getting-the-previous-and-next-record-from-list-using-linq/. But it does not work in my scenerio.
I have considered a group by which I could do except I would miss out on the correct counts to group the Fiscal Year by.