3

I have 2d array with next values.

1 t\2 3
2 f\5 b
4 f\b\g 6 8\9

After split array by \ I want receive

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

I tried this but it doesn't achieve it:

var result = row.Table.AsEnumerable()
        .Where(r => r == row)
        .SelectMany(r => this.projectMapping.Select(pm => r[pm.OriginalName]
        .ToString()
        .Split(this.separator, StringSplitOptions.None)));
4
  • 2d array with next values.?? and what is this format ?1 t\2 3 2 f\5 b Commented Oct 27, 2016 at 19:01
  • Please show an example of how you populate such an array Commented Oct 27, 2016 at 19:03
  • this is examples values in datarow Commented Oct 27, 2016 at 19:03
  • List<string[]> row = new List<string[]>() { new string[] { "1", "t/2", "3" }, new string[] { "2", "f/5", "b"} }; Commented Oct 27, 2016 at 19:07

1 Answer 1

1

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].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.