I have an extension method that produces combinations of sequences. This method can be used to obtain your result.
First, each array in the 2D array must be converted to 2D arrays themselves:
var array = new[]
{
new[] {"1", @"t\2", "3"},
new[] {"2", @"f\5", "b"},
new[] {"4", @"f\b\g", "6", @"8\9"},
};
var splitted = array.Select(a => a.Aggregate(new List<List<string>>(),
(seed, c) =>
{
seed.Add(c.Split(@"\".ToCharArray()).ToList());
return seed;
}));
Now splitted is a sequence of 2D arrays that each have to be combined. To visualize it, the first element of this sequence is:
[1], [t,2], [3]
The extension methods used are:
static class EnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> Combine<T>(this IEnumerable<IEnumerable<T>> listOfLists)
{
IEnumerable<IEnumerable<T>> seed = new List<List<T>> { new List<T>() };
return listOfLists.Aggregate(seed, (r, list) =>
r.SelectMany(a => list.Select(x => a.Append(x))));
}
public static IEnumerable<T> Append<T>(this IEnumerable<T> list, params T[] elements)
{
return list == null ? elements : list.Concat(elements);
}
}
Using the Combine method, the desired result can be obtained by
var result = splitted.SelectMany(s => s.Combine());
And to visualize it:
foreach (var element in result.Select(x => string.Join(", ", x)))
{
Console.WriteLine(element);
}
Giving:
1, t, 3
1, 2, 3
2, f, b
2, 5, b
4, f, 6, 8
4, f, 6, 9
4, b, 6, 8
4, b, 6, 9
4, g, 6, 8
4, g, 6, 9
The idea of the Combine method is that for each sequence entering it, combinations are built cumulatively. Taking the first sequence, [1], [t,2], [3], the result of the first aggregation cycle is [1,t] and [1,2]. Each of these combined with [3] produces [1,t,3] and [1,2,3].