Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge several arrays in a List using Linq?

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

like image 555
Igor Golodnitsky Avatar asked Jul 24 '09 11:07

Igor Golodnitsky


People also ask

How to merge multiple arrays in c#?

CSharp Online Training var myList = new List<int>(); myList. AddRange(arr1); myList. AddRange(arr2); Use the AddRange() method the arrays into the newly created list.

Can LINQ query work with array?

LINQ allows us to write query against all data whether it comes from array, database, XML etc.


2 Answers

You can use SelectMany and then ToArray to do this.

var result = source.SelectMany(i => i).ToArray(); 
like image 150
Daniel Earwicker Avatar answered Sep 25 '22 21:09

Daniel Earwicker


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

like image 25
Marcin Deptuła Avatar answered Sep 26 '22 21:09

Marcin Deptuła