34

I have List<Product[]> and I need to join them into one Product[].

3
  • Please put a little more work into making your questions clearer in the future. Commented Jul 24, 2009 at 11:42
  • 14
    Next time be more helpfull Dave Commented Jul 24, 2009 at 12:58
  • 4
    Nothing wrong with the question. Commented Mar 3, 2017 at 11:48

3 Answers 3

86

You can use SelectMany and then ToArray to do this.

var result = source.SelectMany(i => i).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

5

You can use .Concat() extension method as well, and then .ToArray(): x.Concat(y).Concat(z).ToArray();

Comments

0

For any more complex joins IEnumerable, GetEnumerator and yield are your friends and are super performant. Just for fun, here's some code showing extending Linq with your own custom methods does not have to be scary;

public static class MyLinqExtensions
{
    public static IEnumerable<int> RoundRobin(this IEnumerable<int> a, IEnumerable<int> b)
    {
        var ae = a.GetEnumerator();
        var be = b.GetEnumerator();

        bool aHasNumber = ae.MoveNext();
        bool bHasNumber = be.MoveNext();

        while (aHasNumber || bHasNumber)
        {
            if (aHasNumber)
            {
                yield return ae.Current;
                aHasNumber = ae.MoveNext();
            }

            if (bHasNumber)
            {
                yield return be.Current;
                bHasNumber = be.MoveNext();
            }
        }
    }
}

void Main()
{

    var arr1 = new[] { 1, 2, 3, 4 };
    var arr2 = new[] { 4, 5, 6 };
    var arr3 = new[] { 7, 8, 9};
    var arr4 = new int[] {  };

    // arrays the same size
    Assert.AreEqual(new[] { 4, 7, 5, 8, 6, 9 }, arr2.RoundRobin(arr3).ToArray());

    // array 1 bigger than array 2
    Assert.AreEqual(new[] { 1, 4, 2, 5, 3, 6, 4 }, arr1.RoundRobin(arr2).ToArray());

    // array 1 smaller than 2
    Assert.AreEqual(new[] { 4, 1, 5, 2, 6, 3, 4 }, arr2.RoundRobin(arr1).ToArray());

    // 2 empty, 1 not
    Assert.AreEqual(new[] { 4, 5, 6 }, arr2.RoundRobin(new int[] { }).ToArray());

    // 1 empty, 2 not
    Assert.AreEqual(new[] { 1, 2, 3, 4 }, arr1.RoundRobin(new int[] { }).ToArray());

    // both empty
    Assert.AreEqual(new int[] {  }, arr4.RoundRobin(arr4).ToArray());

    // prove it's Linq
    Assert.AreEqual(new[] { 1, 4, 2}, arr1.RoundRobin(arr2).Take(3).ToArray());

    Console.WriteLine("pass");
}

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.