Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the result of a LINQ query always be guaranteed to be in the correct order?

Tags:

c#

linq

Question: Will the result of a LINQ query always be guaranteed to be in the correct order?

Example:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 

var lowNums = 
  from n in numbers 
  where n < 5 
  select n; 

Now, when we walk through the entries of the query result, will it be in the same order as the input data numbers is ordered?

foreach (var x in lowNums) 
  { 
    Console.WriteLine(x); 
  } 

If someone can provide a note on the ordering in the documentation, this would be perfect.

like image 433
John Threepwood Avatar asked Aug 12 '13 22:08

John Threepwood


1 Answers

  • For LINQ to Objects: Yep.
  • For Parallel LINQ: Nope.
  • For LINQ to Expression Trees (EF, L2S, etc): Nope.
like image 143
Steven Avatar answered Oct 31 '22 08:10

Steven