I have the code below which works for the purpose of what I need but I have an idea that it could be made faster. Please let me know if this code can be improved in any way...
The main issue is that I need to query "data" several time. I just need to make sure that there is no shortcut that I could have used instead.
data= GetData()// this return ILookup<Tuple(string, string, string),string>
foreach (var v0 in data)
{
if (v0.Key.Item3 == string.Empty)
{
//Get all related data
var tr_line = data[v0.Key];
sb.AppendLine(tr_line.First());
foreach (var v1 in data)
{
if (v1.Key.Item2 == string.Empty && v1.Key.Item1 == v0.Key.Item1)
{
var hh_line = data[v1.Key];
sb.AppendLine(hh_line.First());
foreach (var v2 in data)
{
if (v2.Key.Item1 == v0.Key.Item1 && v2.Key.Item2 != string.Empty && v2.Key.Item3 != string.Empty)
{
var hl_sl_lines = data[v2.Key].OrderByDescending(r => r);
foreach (var v3 in hl_sl_lines)
{
sb.AppendLine(v3);
}
}
}
}
}
}
}
Neater, more linq: