Two methods of c# reducing nested loops

Time:2021-12-5
catalogue
  • EG1: a simple key corresponds to a DataRow
  • EG2: an assembled key corresponds to a dictionary of multiple datarows

Of course, in addition to focusing on obvious loops such as for and foreach, we should also focus on obscure loops, such as list. Where and list. Find such as datatable. Select(), LINQ, etc.

To optimize and eliminate business problems, code technology should be considered. When you see the circular search data, move closer to the dictionary as much as possible.

EG1: a simple key corresponds to a DataRow

Before optimization:


using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            ...
            for (int i = 0; i < 1000000; i++)
            {
                var row = table.AsEnumerable().FirstOrDefault(r => r["num"].ToString() == i.ToString());
                ...
            }
        }
    }
}

After optimization:


using System.Data;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            ...
            var dict = table.AsEnumerable().ToDictionary(r => r["num"].ToString());
            for (int i = 0; i < 1000000; i++)
            {
                if (dict.ContainsKey(i.ToString()))
                {
                    var row = dict[i.ToString()];
                }
                ...
            }
        }      
    }
}

EG2: an assembled key corresponds to a dictionary of multiple datarows

Before optimization:


using System.Data;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();   
            ...
            for (int i = 0; i < 1000000; i++)
            {
                var name = "";
                ...
                var rows = table.AsEnumerable().Where(r => r["num"].ToString() == i.ToString() && r["name"].ToString() == name);
            }
        }             
    }
}

After optimization:


using System.Data;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            var group = table.AsEnumerable().GroupBy(r => GetGroupKey(r["num"].ToString(), r["name"].ToString()));
            var dict= group.ToDictionary(r=>r.Key);
            ...
            for (int i = 0; i < 1000000; i++)
            {
                var name = "";
                var key = GetGroupKey(i.ToString(), name);
                if (dict.ContainsKey(key))
                {
                    var rows = dict[key];
                }               
                ...
            }
        }      
       
        private static string GetGroupKey(string _num,string _name)
        {
            return $"num={_num}|name={_name}";
        }
    }
}

Quantitative change will cause qualitative change.

The above are the details of the two methods of c# reducing nested loops. For more information about c# reducing nested loops, please pay attention to other related articles of developeppaer!