Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 822
Valentin Kuzub Avatar asked Aug 18 '11 02:08

Valentin Kuzub


1 Answers

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...

like image 97
Jon Avatar answered Oct 11 '22 12:10

Jon