I have a sanctions api that i need to call, passing in a string of values. these values are constructed as follows:
string searchString = string.Join(" ", myList.ToArray());
// remove any numbers and return complete words
MatcCollection strMatch = Regex.Matches(searchString, @"[^\W\d]+");
var values = strMatch.Cast<Group>().Select(g => g.Value).ToArray();
var combinations = values.Permutations();
Now, that i have the array i need, i call the Permutations method below:
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source)
{
if (source == null)
throw new ArgumentException("source");
return permutations(source.ToArray());
}
the permutations method is:
private static IEnumerable<IEnumerable<T>> permutations<T>(IEnumerable<T> source)
{
var c = source.Count();
if (c == 1)
yield return source;
else
for (int i = 0; i < c; i++)
foreach (var p in permutations(source.Take(i).Concat(source.Skip(i + 1))))
yield return source.Skip(i).Take(1).Concat(p);
}
With a example list of 7 items {one,two,three,four,five,six,seven} this code returns numerous list of 7 elements in lenght.
What I need to create is the following:
First iteration:
return result = one
Second iteration return result = one + ' ' + two
so on and so
I got the above exmple code from a post on SO, so don't know how to change it properly to get what i need.