[英]Create dynamic foreach loop for sequential search type in C#
First analyze my C# code. 首先分析我的C#代码。
string[] shape = { "Round", "Square" };
string[] Size = { "7", "9", "10" };
string[] type = { "Chocklate", "Honey", "Vanila" };
string[] top = { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
string[] msg = { "Happy Birthday", "Own Message", "Get Well Soon" };
List<string> MyPatterns = new List<string>();
for (int i = 0; i < shape.Length; i++)
{
MyPatterns.Add(shape[i]);
for (int j = 0; j < Size.Length; j++)
{
MyPatterns.Add(shape[i] + " - " + Size[j]);
for (int k = 0; k < type.Length; k++)
{
string A = shape[i] + " - " + Size[j] + " - " + type[k];
MyPatterns.Add(A);
for (int l = 0; l < top.Length; l++)
{
string B = shape[i] + " - " + Size[j] + " - " + type[k] + " - " + top[l];
MyPatterns.Add(B);
for (int m = 0; m < msg.Length; m++)
{
string C = shape[i] + " - " + Size[j] + " - " + type[k] + " - " + top[l] + " - " + msg[m];
MyPatterns.Add(C);
}
}
}
}
}
Now above example has 5 static arrays shape, size, type, top, msg and above code shows every combination is possible between these arrays. 现在,上面的示例具有5个静态数组,它们的形状,大小,类型,顶部,味精和上述代码显示了这些数组之间的每种组合都是可能的。 Like Round Round - 7 Round - 7 - Chocklate like this all possible combination.
像Round-7 Round-7-Chocklate这样的所有可能组合。
Screenshot : 屏幕截图:
Currently I have static arrays shape, size, type, etc.. So I am manually writing foreach loop for each array inside upper array. 当前,我具有静态数组的形状,大小,类型等。因此,我正在为上层数组中的每个数组手动编写foreach循环。
Now suppose these arrays comes dynamically. 现在假设这些数组是动态产生的。 I Don't know count the will be for example sometimes it must be shape and size some time its must be shape, size and type, and sometime they are completely different.
我不知道计数会是多少,例如有时它必须是形状和大小,有时它必须是形状,大小和类型,有时它们是完全不同的。
So how can I dynamically Generates foreach loop for dynamic arrays ? 那么,如何为动态数组动态生成foreach循环?
Group those dynamic arrays into an array, so you have an array of arrays (AKA Jagged Array . This way you can loop through the parent array using foreach
. 将这些动态数组分组为一个数组,这样就得到了一个数组数组(又名“ 锯齿数组” 。这样,您可以使用
foreach
父数组。
string[][] arr = new string[5][];
arr[0] = new string [] { "Round", "Square" };
arr[1] = new string [] { "7", "9", "10" };
arr[2] = new string [] { "Chocklate", "Honey", "Vanila" };
arr[3] = new string [] { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
arr[4] = new string [] { "Happy Birthday", "Own Message", "Get Well Soon" };
foreach (var a in arr) {
Console.WriteLine("Array count: " + a.Length);
//Now you can loop through each child array.
foreach (string val in a) {
//Do your stuff.
Console.WriteLine("\t" + val);
}
}
This a typical candidate for a recursive implementation: 这是递归实现的典型候选人:
static void Main(string[] args)
{
string[] shape = { "Round", "Square" };
string[] size = { "7", "9", "10" };
string[] type = { "Chocklate", "Honey", "Vanila" };
string[] top = { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
string[] msg = { "Happy Birthday", "Own Message", "Get Well Soon" };
IEnumerable<string[]> productRows = CartesianProductRecursive(new []{ shape, size, type, top, msg });
foreach (var row in productRows)
{
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", row[0], row[1], row[2], row[3], row[4]);
}
}
public static IEnumerable<string[]> CartesianProductRecursive(IEnumerable<string[]> dimValues)
{
if (!dimValues.Any())
yield break;
string[] firstDimValues = dimValues.First();
IEnumerable<string[]> lastDimsProduct = CartesianProductRecursive(dimValues.Skip(1));
if (!lastDimsProduct.Any())
yield return firstDimValues;
foreach (string firstDimValue in firstDimValues)
{
foreach (string[] lastDimsProductRow in lastDimsProduct)
{
yield return new[] {firstDimValue}.Concat(lastDimsProductRow).ToArray();
}
}
}
If you are performing tests, you can use NUnit cartesian product feature or NCase test case generator . 如果要执行测试,则可以使用NUnit笛卡尔积功能或NCase测试用例生成器 。 Both allow to reduce the amount of test cases by generating the pairwise product instead of the cartesian product.
两者都允许通过生成成对乘积而不是笛卡尔乘积来减少测试用例的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.