tags:

views:

336

answers:

5

I got a linq query who returns a IEnumarable<List<int>> but i need to return, only List<int> so i want to merge all my record in my IEnumerable<List<int>> to only one array.

Example :

IEnumerable<List<int>> iList = from number in (from no in Method() select no) select number;

I want to take all my result IEnumerable<List<int>> to only one List<int>

Hence, from source arrays: [1,2,3,4] and [5,6,7]

I want only one array [1,2,3,4,5,6,7]

Thanks

+10  A: 

Try SelectMany()

var result = iList.SelectMany( i => i );
Mike Two
Thanks it's work
Cédric Boivin
+3  A: 

Like this?

var iList = Method().SelectMany(n => n);
mquander
Yes thanks for your answer to
Cédric Boivin
A: 

If you have a List<List<int>> k you can do

List<int> flatList= k.SelectMany( v => v).ToList();
Daniel
+2  A: 
iList.SelectMany(x => x).ToArray()
Dylan Beattie
+1 for getting the array part correct which everyone else missed.
recursive
+7  A: 

With query syntax:

var values =
from inner in outer
from value in inner
select value;
recursive
+1 for the alternate syntax
Tim Jarvis