Question

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);
                        }
                    }
                }
            }
        }
    }
 } 
Was it helpful?

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

OTHER TIPS

First of all, try to avoid using Tuple for this kind of code, because, even to you, a few months from now, this code will be incomprehensible. Make a class, or even better, an immutable struct with the correct property names. Even the fastest code is worthless if it is not maintainable.

That said, you have three nested loops that iterate the same collection. It would be plausible that a sorted collection will perform faster, as you will need to compare only with adjacent items.

Please try to explain what you are trying to accomplish, so someone would try to offer more specific help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top