I've been trying to think the best route to do a loop for this data. I have a table that can be seen below and need to iterate through all column configurations. The column count is dynamic. Results should be:
Apple-Broccoli-Ice Cream
Apple-Broccoli-Pie
Apple-Carrots-Ice Cream
Apple-Carrots-Pie
Apple-Peas-Ice Cream
Apple-Peas-Pie
Apple-Green Beans-Ice Cream
Apple-Green Beans-Pie
Orange-Broccoli-Ice Cream
Orange-Broccoli-Pie
Orange-Carrots-Ice Cream
Orange-Carrots-Pie
Orange-Peas-Ice Cream
Orange-Peas-Pie
Orange-Green Beans-Ice Cream
Orange-Green Beans-Pie
My "manual" code is below, but I would like it to be more dynamic regarding columns (there could be 2,3,5, etc.)
What is the best way to make a dynamic loop when the columns vary?
Note: I have an object "Configuration" and a list of those objects called "configs"
Image of Configuration 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));
}
}
}
}
}
}
}
}
}
}
You are looking for the Cartesian product to produce the final outputs.
So first convert the table (or data source you have) into a list of lists of string, then apply the cartesian product to every list in the collection.
//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;
}
you might use this method to convert your table into lists
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, "-");
EDIT: OK, sorry, missed the dynamic thing. Here you go. EDIT1: Not sure why one would down vote this example. Works fine for me. Here us update the make the algo work with the datatable.
static void Main(string[] args)
{
System.Data.DataTable dataGridConfigTable = new System.Data.DataTable();
dataGridConfigTable.Columns.Add("Parameter1");
dataGridConfigTable.Columns.Add("Parameter2");
dataGridConfigTable.Columns.Add("Parameter3");
var row = dataGridConfigTable.NewRow();
row[0] = "Apple";
row[1] = "Broccoli";
row[2] = "Ice Cream";
dataGridConfigTable.Rows.Add(row);
row = dataGridConfigTable.NewRow();
row[0] = "Orange";
row[1] = "Carrots";
row[2] = "Pie";
dataGridConfigTable.Rows.Add(row);
row = dataGridConfigTable.NewRow();
row[1] = "Peas";
dataGridConfigTable.Rows.Add(row);
row = dataGridConfigTable.NewRow();
row[1] = "Grean Beans";
dataGridConfigTable.Rows.Add(row);
var dynamicList = new List<List<string>>();
foreach (System.Data.DataColumn dc in dataGridConfigTable.Columns)
{
dynamicList.Add(dataGridConfigTable.AsEnumerable().Select(s => s.Field<string>(dc.ColumnName)).Where(x => x != null).ToList());
}
List<string> result = null;
dynamicList.ForEach(x => BuildString(ref result, x));
result.ForEach(i => Console.WriteLine(i));
Console.ReadLine();
}
static void BuildString(ref List<string> param1List, List<string> param2List)
{
if (param1List == null)
{
param1List = param2List;
return;
}
List<string> result = new List<string>();
param1List.ForEach(x => param2List.ForEach(y => result.Add(String.Format("{0} - {1}", x, y))));
param1List = result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.