I use LINQ to Objects instructions on an ordered array. Which operations shouldn\'t I do to be sure the order of the array is not changed?
I found a great answer in a similar question which references official documentation. To quote it:
For Enumerable
methods (LINQ to Objects, which applies to List
), you can rely on the order of elements returned by Select
, Where
, or GroupBy
. This is not the case for things that are inherently unordered like ToDictionary
or Distinct
.
From Enumerable.GroupBy documentation:
The
IGrouping
objects are yielded in an order based on the order of the elements in source that produced the first key of eachIGrouping
. Elements in a grouping are yielded in the order they appear insource
.
This is not necessarily true for IQueryable
extension methods (other LINQ providers).
Source: Do LINQ's Enumerable Methods Maintain Relative Order of Elements?