386

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

5 Answers 5

700

Try SelectMany()

var result = iList.SelectMany( i => i );
4
  • 10
    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. :-) Dec 19, 2013 at 18:51
  • For a while I was afraid that I was the only one who ever needed this. Thanks Mike! Apr 14, 2015 at 8:52
  • 14
    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 9, 2017 at 12:15
  • 1
    @Andy argh! If only the language designers had given us some way to 'extend' an IEnumerable ourselves! Aug 3, 2022 at 17:50
91

With query syntax:

var values =
from inner in outer
from value in inner
select value;
4
  • Thanks exact syntax I was looking for, and so many SO answers list something else more verbose. Feb 4, 2014 at 21:19
  • This is much, much better than SelectMany. More clear exactly what's going on IMO, thanks for pointing this out! Sep 14, 2016 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, 2016 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, 2022 at 15:02
24
iList.SelectMany(x => x).ToArray()
4
  • 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). Dec 19, 2013 at 18:55
  • 2
    Presumably the circumstances in this case require arrays, since that was specified in the question.
    – recursive
    Dec 19, 2013 at 21:49
  • 9
    @recursive, if we are nitpicking, the OP says that he needs to return List<int>, so .ToList() would then be the correct choice.
    – MEMark
    Feb 26, 2014 at 16:48
  • @MEMark OP also states " to only one array" Feb 26, 2019 at 11:16
14

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

List<int> flatList= k.SelectMany( v => v).ToList();
13

Like this?

var iList = Method().SelectMany(n => n);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.