391

I have a LINQ query which returns IEnumerable<List<int>> but i want 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

Liam
  • 27,717
  • 28
  • 128
  • 190
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98

5 Answers5

712

Try SelectMany()

var result = iList.SelectMany( i => i );
kalyfe
  • 1,005
  • 1
  • 16
  • 33
Mike Two
  • 44,935
  • 9
  • 80
  • 96
  • 11
    Thanks, I always forget this one -- I know it's there, but I just spend way too much time Googling for it every time I need to use it. Bookmarking this answer. :-) – BrainSlugs83 Dec 19 '13 at 18:51
  • For a while I was afraid that I was the only one who ever needed this. Thanks Mike! – Arnab Chakraborty Apr 14 '15 at 08:52
  • 16
    Is there some alternate syntax for `SelectMany( i => i )` ? I've seen this syntax used a lot but it seems a degenerate use of the select feature, so I would have expected the language designers to come up with a shortcut syntax specifically for lists of lists – Andy Feb 09 '17 at 12:15
  • 1
    @Andy argh! If only the language designers had given us some way to 'extend' an `IEnumerable` ourselves! – Jonesopolis Aug 03 '22 at 17:50
92

With query syntax:

var values =
from inner in outer
from value in inner
select value;
recursive
  • 83,943
  • 34
  • 151
  • 241
  • Thanks exact syntax I was looking for, and so many SO answers list something else more verbose. – SilverSideDown Feb 04 '14 at 21:19
  • This is much, much better than SelectMany. More clear exactly what's going on IMO, thanks for pointing this out! – Bryan Rayner Sep 14 '16 at 15:49
  • 8
    Personally I always find the query syntax version much less intuitive than the method calling version. When Resharper offers to convert loops to LINQ expressions if it gives me the query syntax I always go for undo. – bikeman868 Sep 23 '16 at 23:50
  • Pretty funny how divided "the community" is on this - to me this is completely unreadable, though I do realize it's habit with the arrow function syntax and that this is a valid solution. – VSO Dec 13 '22 at 15:02
24
iList.SelectMany(x => x).ToArray()
Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
  • 9
    @recursive What did everyone else miss? `.ToArray()`? -- That's kind of circumstantial -- if you only need to iterate once -- or if the items are likely to change, then `.ToArray()` is definitely not what you want. But with static items that you're going to enumerate multiple times, `.ToList()` or `.ToArray()` will give a performance improvement (at the cost of slightly higher memory usage, which, is usually a pretty good deal). – BrainSlugs83 Dec 19 '13 at 18:55
  • 2
    Presumably the circumstances in this case require arrays, since that was specified in the question. – recursive Dec 19 '13 at 21:49
  • 9
    @recursive, if we are nitpicking, the OP says that he needs to return `List`, so `.ToList()` would then be the correct choice. – MEMark Feb 26 '14 at 16:48
  • @MEMark OP also states " to only one array" – StefanJanssen Feb 26 '19 at 11:16
14

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

List<int> flatList= k.SelectMany( v => v).ToList();
Daniel
  • 22,363
  • 9
  • 64
  • 71
13

Like this?

var iList = Method().SelectMany(n => n);
mqp
  • 70,359
  • 14
  • 95
  • 123