Search code examples
c#linqilookup

C# Lookup ptimisation suggestion are welcome


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);
                        }
                    }
                }
            }
        }
    }
 } 

Solution

  • Neater, more linq:

            var data = GetData();
    
            foreach (var v0 in data)
            {
                if (v0.Key.Item3 != string.Empty) continue;
    
                //Get all related data 
                var tr_line = data[v0.Key];
                sb.AppendLine(tr_line.First());
    
                var hhLines = from v1 in data
                              where v1.Key.Item2 == string.Empty &&
                                    v1.Key.Item1 == v0.Key.Item1
                              select data[v1.Key];
    
                foreach (var hh_line in hhLines)
                {
                    sb.AppendLine(hh_line.First());
    
                    var grouping = v0;
                    var enumerable = from v2 in data
                                     where v2.Key.Item1 == grouping.Key.Item1 &&
                                           v2.Key.Item2 != string.Empty &&
                                           v2.Key.Item3 != string.Empty
                                     select data[v2.Key].OrderByDescending(r => r)
                                     into hl_sl_lines from v3 in hl_sl_lines select v3;
    
                    foreach (var v3 in enumerable)
                    {
                        sb.AppendLine(v3);
                    }
                }
            }