The following code is pretty ugly. I suspect that there is a more functional and elegant way of achieving this result.
var lines =
new[]{
new{Head="A",Value="1"},
new{Head="",Value="2"},
new{Head="",Value="3"},
new{Head="",Value="4"},
new{Head="B",Value="5"},
new{Head="B",Value="6"},
new{Head="C",Value="7"},
new{Head="",Value="8"},
new{Head="D",Value="9"},
new{Head="",Value="10"}};
string currentHead="";
string currentValue="";
lines.Select(line=>{
string newHead;
string newValue;
if (String.IsNullOrEmpty(line.Head))
{
newHead = currentHead;
} else
{
newHead = line.Head;
currentHead = line.Head;
}
if (String.IsNullOrEmpty(line.Value))
{
newValue = currentValue;
} else
{
newValue = line.Value;
currentValue = line.Value;
}
return new{Head=newHead,Value=newValue};}).ToList();
The desired output is to produce
A,1
A,2
A,3
A,4
B,5
B,6
C,7
C,8
D,9
D,10
Value
s there? Can't you compute them? They seem to be 1,2,3,…,10. \$\endgroup\$