18

I have a List<Person> and instead want to convert them for simple processing to a List<string>, doing the following:

List<Person> persons = GetPersonsBySeatOrder();
List<string> seatNames = persons.Select(x => x.Name).ToList();

Console.WriteLine("First in line: {0}", seatNames[0]);

Is the .Select() statement on a LINQ to Objects object guaranteed to not change the order of the list members? Assuming no explicit distinct/grouping/ordering is added

Also, if an arbitrary .Where() clause is used first, is it still guaranteed to keep the relative order, or does it sometimes use non-iterative filtering?


As Fermin commented above, this is essentially a duplicate question. I failed on selecting the correct keywords to search stackoverflow

Preserving order with LINQ

Community
  • 1
  • 1
arserbin3
  • 6,010
  • 8
  • 36
  • 52
  • 5
    Good details of linq queries and ordering here: http://stackoverflow.com/questions/204505/preserving-order-with-linq – Fermin Jan 09 '13 at 16:13

3 Answers3

2

It depends on the underlying collection type more than anything. You could get inconsistent ordering from a HashSet, but a List is safe. Even if the ordering you want is provided implicitly, it's better to define an explicit ordering if you need it though. It looks like you're doing that judging by the method names.

Samantha Branham
  • 7,350
  • 2
  • 32
  • 44
1

In current .Net implementation it use such code. But there are no guarantee that this implementation will be in future.

private static IEnumerable<TResult> SelectIterator<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
{
  int index = -1;
  foreach (TSource source1 in source)
  {
    checked { ++index; }
    yield return selector(source1, index);
  }
}
Andrew
  • 541
  • 3
  • 5
  • 2
    I doubt that the internal implementation of such a widely used method as .Select() would be changed to return objects in a different order. – Fermin Jan 09 '13 at 16:23
  • 1
    Database rows have no guaranteed order. Lists do. It should absolutely depend on the underlying collection type (i.e. the underlying collection type's *enumerator*), and list order should be preserved, guaranteed. Select would use the List type's enumerator, and currently it preserves order, guaranteed. – Triynko Dec 12 '13 at 07:26
  • @Fermin there actually was a change in `Join` between .NET and .NET Core, but it was corrected once discovered. – Cory Nelson Feb 08 '19 at 04:40
1

Yes, Linq Select is guaranteed to return all its results in the order of the enumeration it is passed. Like most Linq functions, it is fully specified what it does. Barring handling of errors, this might as well be the code for Select:

IEnumerable<Y> Select<X, Y>(this IEnumerable<X> input, Func<X, Y> transform)
{
    foreach (var x in input)
        yield return transform(x);
}

But as Samantha Branham pointed out, the underlying collection might not have an intrinsic order. I've seen hashtables that rearrange themselves on read.

Joshua
  • 40,822
  • 8
  • 72
  • 132