3

I'm working with an object declared as shown.

DataCollection<Entity> collection = GetDataCollection(...);

Then, I'm picking out two sets of data as below.

IEnumerable<String> 
  array_1 = collection.Select(
    element => element.Attributes["attribute_1"] as String),
  array_2 = collection.Select(
    element => element.Attributes["attribute_2"] as String);

I'm terribly curious if those two arrays are guaranteed to have the same order. I.e. if their separate orders must be the same as in the original sequence. Or, at the very least, if the order would change for any reason, will the ith element in the first separate list still correspond to the ith element in the other?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 2
    to be safe, get both attrs at once `var arr = collection.Select(element => new { Attr1 = element.Attributes["att1"] as String, Attr2 = element.Attributes["att2"] as String });` – L.B Sep 28 '12 at 14:03

2 Answers2

2

They're not guaranteed to be the same. In reality, you'll probably notice that the order is typically consistent with the original collection.

If you're worried about consistency, just add an OrderBy clause to get the order you need (or at least one that is consistent between the two resulting collections).

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 1
    Is it possible to `OrderBy` them without specifying an explicit ordering function? By that I mean an ordering function like `OrderType.WhateverYouHadBefore` or `OrderType.JustAsLongAsYouAreConsistent´. Or am I talking jibrish? – Konrad Viltersten Sep 28 '12 at 14:05
  • @Chamster - Unless the original set was ordered a certain way, no. – Justin Niessner Sep 28 '12 at 14:16
  • @Chamster In linq-to-objects, sure. `Select` will always maintain ordering for any `IEnumerable`. An `IQueryable` `Select` call is done in the database, so it depends entirely on that provider, and that database design, for whether or not the queries will always have the same ordering. – Servy Sep 28 '12 at 15:05
  • @Servy - Do you have a link to documentation that guarantees LINQ To Objects will maintain order? The current implementation may, but that's far from a guarantee that it will remain that way. Building code on an implied promise is a good way to ensure that it will break down the line. – Justin Niessner Sep 28 '12 at 15:08
  • @JustinNiessner It is defined behavior that select maintains order, not just an implementation detail. – Servy Sep 28 '12 at 15:13
  • @Servy - That's why I was asking for documentation. – Justin Niessner Sep 28 '12 at 15:19
0

Check this answer:

Preserving order with LINQ

It does appear, at least from this answer, that Select calls are guaranteed to be the same order.

I'd still follow this with an OrderBy(...). It seems like you can never be sure with LINQ calls, so if someone comes in and adds an additional LINQ call, they may not know that you were expecting them to be the same order.

Community
  • 1
  • 1
DanTheMan
  • 3,277
  • 2
  • 21
  • 40