复杂的 for 循环 c# - 迭代每一列行以创建 "Configurations"

Complex for loop c# - Iterating each column row to create "Configurations"

我一直在思考为这些数据做循环的最佳途径。我有一个 table 可以在下面看到,需要遍历所有列配置。列数是动态的。结果应该是:

苹果-西兰花-冰淇淋
苹果西兰花派
苹果-胡萝卜-冰淇淋
苹果胡萝卜派
苹果豌豆冰淇淋
苹果豌豆派
苹果-青豆-冰淇淋
苹果绿豆派
橙色-西兰花-冰淇淋
橙色西兰花派
橙色-胡萝卜-冰淇淋
橙色胡萝卜派
橙豌豆冰淇淋
橙豌豆派
橙-绿豆-雪糕
橙绿豆饼

我的 "manual" 代码在下面,但我希望它在列方面更具动态性(可能有 2、3、5 等)

当列不同时创建动态循环的最佳方法是什么?

注意:我有一个对象 "Configuration" 和一个名为 "configs"

的对象列表

配置图片Table

List<Configuration> configs = new List<Configuration>();

// iterate left to right
for (int i1 = 0; i1 < dataGridConfigTable.Rows.Count - 1; i1++)
{
    string param1 = dataGridConfigTable.Rows[i1].Cells[0].Value.ToString() ?? string.Empty;

    if (!string.IsNullOrEmpty(param1))
    {
        for (int i2 = 0; i2 < dataGridConfigTable.Rows.Count - 1; i2++)
        {
            string param2 = dataGridConfigTable.Rows[i2].Cells[1].Value.ToString() ?? string.Empty;

            if (!string.IsNullOrEmpty(param2))
            {
                for (int i3 = 0; i3 < dataGridConfigTable.Rows.Count - 1; i3++)
                {
                    string param3 = dataGridConfigTable.Rows[i3].Cells[2].Value.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(param3))
                    {
                        for (int i4 = 0; i4 < dataGridConfigTable.Rows.Count - 1; i4++)
                        {
                            string param4 = dataGridConfigTable.Rows[i4].Cells[3].Value.ToString() ?? string.Empty;

                            if (!string.IsNullOrEmpty(param4))
                            {
                                for (int i5 = 0; i5 < dataGridConfigTable.Rows.Count - 1; i5++)
                                {
                                    string param5 = dataGridConfigTable.Rows[i5].Cells[4].Value.ToString() ?? string.Empty;

                                    if (!string.IsNullOrEmpty(param5))
                                    {
                                        configs.Add(new Configuration(param1, param2, param3, param4, param5));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

您正在寻找 Cartesian product 来生成最终输出。

因此,首先将 table(或您拥有的数据源)转换为字符串列表列表,然后将笛卡尔积应用于集合中的每个列表。

    //this function produce the cartesian product of two lists
    List<string> CartesianProduct(List<string> lst1, List<string> lst2, string seperator)
    {
        var res = new List<string>();
        for (int i = 0; i < lst1.Count; i++)
        {
            for (int j = 0; j < lst2.Count; j++)
            {
                res.Add(lst1[i] + seperator + lst2[j]);
            }
        }
        return res;
    }

    //This function apply the cartesian product to all lists
    List<string> CartesianProduct(List<List<string>> lsts, string seperator)
    {
        List<string> res = lsts[0];

        for (int i = 1; i < lsts.Count; i++)
        {
            res = CartesianProduct(res, lsts[i], seperator);
        }
        return res;
    }

您可以使用此方法将您的 table 转换为列表

       var dt1 = new DataTable(); //your data source
        var lsts = new List<List<string>>();

        for (int i = 0; i < dt1.Columns.Count; i++)
        {
            var singleColumnLst = new List<string>();
            for (int j = 0; j < dt1.Rows.Count; j++)
            {
                singleColumnLst.Add((string)dt1.Rows[i][j]);
            }
            lsts.Add(singleColumnLst);
        }

        //final results
        var allItems = CartesianProduct(lsts, "-");