Does LINQ preserve order of elements in arrays?

Yes, it does (in expected cases), here the list:


Preserves Order Absolutely.
  • AsEnumerable
  • Cast
  • Concat
  • Select
  • ToArray
  • ToList
Preserves Order. Elements are filtered, but not re-ordered.
  • Except
  • Intersect
  • OfType
  • Skip
  • SkipWhile
  • Take
  • TakeWhile
  • Where
  • Zip (new in .net 4)
Destroys Order
  • Distinct
  • ToDictionary
  • ToLookup
Redefines Order Explicitly
  • OrderBy
  • OrderByDescending
  • Reverse
  • ThenBy
  • ThenByDescending
Redefines Order according to some rules.
  • GroupBy - The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. Elements in a grouping are yielded in the order they appear in source.
  • GroupJoin - GroupJoin preserves the order of the elements of outer, and for each element of outer, the order of the matching elements from inner.
  • Join - preserves the order of the elements of outer, and for each of these elements, the order of the matching elements of inner.
  • SelectMany - for each element of source, selector is invoked and a sequence of values is returned.
  • Union - When the object returned by this method is enumerated, Union enumerates first and second in that order and yields each element that has not already been yielded.
ref. http://stackoverflow.com/questions/204505/preserving-order-with-linq

Commenti

Post più popolari