LINQ transforming hierachical data to flat list 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=&gt;{
        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
You need to look through your code with an analytical eye and discover the pattern that you are implementing. I call it default if (null or) empty.

So I created an extension method to implement that pattern.

public static string DefaultIfEmpty(this string source, string defaultValue)
{
    return string.IsNullOrEmpty(source) ? defaultValue : source;
}


(Note of caution: you may want to choose a different name for it since this method already exists in LINQ; technically, this is an overload, which could create problems if a future version of C# adds this signature.)  

This makes it possible to adapt the LINQ expression:

var currentHead = string.Empty;
var currentValue = string.Empty;
var result = lines.Select(
    line =&gt;
    {
        currentHead = line.Head.DefaultIfEmpty(currentHead);
        currentValue = line.Value.DefaultIfEmpty(currentValue);
        return new { Head = currentHead, Value = currentValue };
    }).ToList();


Sweet, isn't it?