I want to generate a list of all possible combinations of a list of strings (it\'s actually a list of objects, but for simplicity we\'ll use strings). I need this list so th
One more recursive solution. From AllCombinations
in below code, you will get all possible combinations.
Logic:
Code:
public class Combination
{
private IEnumerable list { get; set; }
private int length;
private List> _allCombination;
public Combination(IEnumerable _list)
{
list = _list;
length = _list.Count();
_allCombination = new List>();
}
public IEnumerable> AllCombinations
{
get
{
GenerateCombination(default(int), Enumerable.Empty());
return _allCombination;
}
}
private void GenerateCombination(int position, IEnumerable previousCombination)
{
for (int i = position; i < length; i++)
{
var currentCombination = new List();
currentCombination.AddRange(previousCombination);
currentCombination.Add(list.ElementAt(i));
_allCombination.Add(currentCombination);
GenerateCombination(i + 1, currentCombination);
}
}
}