获得所有参数组合

ibi*_*iza 4 c# combinations permutation

我有一个带有可能值的参数列表:

// Definition of a parameter
public class prmMatrix
{
    public string Name { get; set; }
    public List<string> PossibleValues { get; set; }

    public prmMatrix(string name, List<string> values)
    {
        Name = name;
        PossibleValues = values;
    }
}

//[...]

// List of params       
List<prmMatrix> lstParams = new List<prmMatrix>();

lstParams.Add(new prmMatrix("Option A", new List<string>() { "Yes", "No" }));
lstParams.Add(new prmMatrix("Option B", new List<string>() { "Positive", "Negative" }));
Run Code Online (Sandbox Code Playgroud)

我希望所有参数组合都可能,例如:

[Option A:Yes][Option B:Positive]
[Option A:Yes][Option B:Negative]
[Option A:No][Option B:Positive]
[Option A:No][Option B:Negative]
Run Code Online (Sandbox Code Playgroud)

C#中最好的方法是什么?

Ben*_*igt 5

递归这很容易:

void ImplCombinations(List<prmMatrix> plist, string built, int depth, List<string> results)
{
    if (depth >= plist.Count()) {
        results.Add(built);
        return;
    }

    prmMatrix next = plist[depth];
    built += "[" + next.Name + ":";
    foreach (var option in next.PossibleValues)
        ImplCombinations(plist, built + option + "]", depth + 1, results);
}

List<string> GetCombinations(List<prmMatrix> plist)
{
    List<string> results = new List<string>();
    ImplCombinations(plist, "", 0, results);
    return results;
}
Run Code Online (Sandbox Code Playgroud)