I have the following list:
List<Decimal?> values = new List<Decimal?> { 3, null, 2, 1, 4, 3 };
I need to create a new list that contains for each item the Maximum between the item and the two previous items so:
List<Decimal?> maximums = new List<Decimal?> { 3, 2, 4, 4 };
3 = Maximum of { 3, null, 2 }
2 = Maximum of { null, 2, 1 }
4 = Maximum of { 2, 1, 4 }
4 = Maximum of { 1, 4, 3 }
My idea is to have a loop and in each iteration do:
for (int i = 2; i < values.Count(); i++) {
Decimal? maximum = values.Skip(i).Take(3).Max();
maximums.Add(maximum);
}
Is there a way to do this using only Linq?