430

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

782

Try SelectMany()

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

4 Comments

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. :-)
For a while I was afraid that I was the only one who ever needed this. Thanks Mike!
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 argh! If only the language designers had given us some way to 'extend' an IEnumerable ourselves!
93

With query syntax:

var values =
from inner in outer
from value in inner
select value;

4 Comments

Thanks exact syntax I was looking for, and so many SO answers list something else more verbose.
This is much, much better than SelectMany. More clear exactly what's going on IMO, thanks for pointing this out!
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.
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.
25
iList.SelectMany(x => x).ToArray()

4 Comments

@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).
Presumably the circumstances in this case require arrays, since that was specified in the question.
@recursive, if we are nitpicking, the OP says that he needs to return List<int>, so .ToList() would then be the correct choice.
@MEMark OP also states " to only one array"
15

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

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

Comments

13

Like this?

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

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.