Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does IEnumerable<TSource> Concat<TSource> preserve the order of elements?

Tags:

c#

.net

linq

Assume two lists, A and B so that A = (1,2,3) and B = (4,5,6). Will A.Concat(B) preserve the order so that the result is (1,2,3,4,5,6)?

like image 583
sduplooy Avatar asked Feb 03 '09 15:02

sduplooy


People also ask

Does concat preserve order?

Ordering in SQL is the final step before results are produced. For this reason, the Concat operator is implemented by using UNION ALL and does not preserve the order of its arguments.

Does ToList preserve order?

The simple answer is no, ToList will just loop over the source enumerable and keep the same order.

Does Groupby preserve order?

Groupby preserves the order of rows within each group. When calling apply, add group keys to index to identify pieces. Reduce the dimensionality of the return type if possible, otherwise return a consistent type.

Does Linq Groupby preserve order?

Found answer on MSDN: Yes.


2 Answers

Yes. IEnumerable.Concat will simply turn two list into a single list by attaching one to the end of the other. Order within each list will be preserved.

like image 70
JaredPar Avatar answered Oct 08 '22 02:10

JaredPar


Yes, that's pretty much what concatenation means.

Obligatory MSDN quote: (Enumerable.Concat)

Return Value

Type: System.Collections.Generic.IEnumerable(TSource)

An IEnumerable(T) that contains the concatenated elements of the two input sequences.

like image 21
Tamas Czinege Avatar answered Oct 08 '22 02:10

Tamas Czinege