Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Guarantees Ordering with SelectMany?

Tags:

c#

linq

I have an array of ordered enumerables IorderedEnumerable<T>[] foo and I want to flatten it so that the ordered enumerables of foo are concatenated together in the order they are stored in the array.

For example {{1, 2, 3}, {4, 5}, {6}} => {1, 2, 3, 4, 5, 6}

Can I do this by IOrderedEnumerable<T> bar = foo.SelectMany(x => x);, or does LINQ not guarantee how order is handled when flattening?

like image 502
Void Star Avatar asked Aug 31 '15 15:08

Void Star


1 Answers

All LINQ to Objects methods (except, obviously, OrderBy() and ToDictionary()) will preserve source ordering.

like image 116
SLaks Avatar answered Sep 21 '22 13:09

SLaks