4

I have a list of parameters with possible values :

// 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" }));

I would like to have all the combinations of parameters possible, ex.:

[Option A:Yes][Option B:Positive]
[Option A:Yes][Option B:Negative]
[Option A:No][Option B:Positive]
[Option A:No][Option B:Negative]

What's the best way in C# ?

Bruno
  • 4,685
  • 7
  • 54
  • 105
  • possible duplicate of [Generating Permutations using LINQ](http://stackoverflow.com/questions/4319049/generating-permutations-using-linq) – Austin Salonen Nov 16 '11 at 21:23

5 Answers5

5

This is pretty easy with recursion:

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;
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

Use a Cross Join to get the cartesian product between your data sets. This can be accomplished very easily using LINQ. e.g.

var lstValues = new List<string>() { "Yes", "No" };
var lstValues2 = new List<string>() { "Positive", "Negative" };

var crossJoinQuery =
   from a in lstValues
   from b in lstValues2
   select new { a, b };

foreach (var o in crossJoinQuery)
{
   Console.WriteLine(string.Format("{0},{1}", o.a , o.b));
}

This will give you a combination of all values. You can then do whatever you want with the result set.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • 1
    That's what I was thinking too, but then I re-read the question and realized that the 2 lists are not known at compile time ... there's a list of lists. – Scott Rippey Nov 16 '11 at 22:33
1
list<item> recurseMe(list<category> parameters, item building)
{
  if (parameters.isEmpty())
  {
    return item;
  }
  category param = parameters[0];
  list<item> ret = new list<item>();
  for(int i = 0; i < param.possibleValues; i++)
  {
    ret.add(recurseMe(parameters without param, item with param[i]);
  }
  return ret;
}

A category would be one parameter of your item, and contain all the possible values for it.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
1

Recursion time, then.

private prmMatrix[] _allParams;
private List<String> _allCombos;

public List<String> EnumarateAllCombinations()
{
   _allCombos = new List<String>();
   EnumParams(0, "");
   return _allCombos;
}

private void EnumParams(int paramNum, string paramValues)
{
    if(paramNum >= allParams.Length)
    {
        _allCombos.add(paramValues);
    }
    else
    {
        prmMatrix current = _allParams[paramNum];
        foreach(string val in current.PossibleValues)
            EnumParams(paramNum+1, paramValues + "[" + val + "]");
    }
 }
}
zmbq
  • 38,013
  • 14
  • 101
  • 171
0

My suggestion is:

foreach (var optionA in lstValues)
{
    foreach (var optionB in lstValues2)
    {
        // Do someting
    }
}
Fischermaen
  • 12,238
  • 2
  • 39
  • 56