Valentin Kuzub
Valentin Kuzub

Reputation: 12093

Is Linq to objects join default order specified when no order by is used?

I got following query

IEnumerable<string> values = from first in goodOrdered
join second in badOrdered on first.ToLower() equals
second.ToLower()
select second;

Currently my tests show that end result is actually goodOrdered, like I want it. Can I expect that to always be true or I should provide an order by statement that will force to keep goodOrdered order ( it will make query more complex, because goodOrdered can look like 1, 9, 2, 7, 6)?

Upvotes: 6

Views: 1073

Answers (1)

Jon
Jon

Reputation: 16726

Yes, it does. According to the MSDN documentation:

Join preserves the order of the elements of outer, and for each of these elements, the order of the matching elements of inner.

Of course, if it was not specified in the documentation it would be an entirely different matter. Another issue you may want to consider is developers who will be responsible for maintaining the code, and whether you would expect them to know/remember that Join's contract includes preserving order of the outer sequence...

Upvotes: 10

Related Questions