简体   繁体   English

LINQ查询的结果是否始终保证正确的顺序?

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

Question: Will the result of a LINQ query always be guaranteed to be in the correct order? 问题:LINQ查询的结果是否始终保证正确的顺序?

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? 现在,当我们通过查询结果的项目走,不会是在相同的顺序输入数据numbers是有序?

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

If someone can provide a note on the ordering in the documentation, this would be perfect. 如果有人可以提供文档中的订单说明,这将是完美的。

I think the order of elements retrieved by a LINQ is preserved, at least for LINQ to Object , for LINQ to SQL or Entity , it may depend on the order of the records in the table. 我认为LINQ检索的元素的顺序是保留的,至少对于LINQ to Object ,对于LINQ to SQL or Entity ,它可能取决于表中记录的顺序。 For LINQ to Object , I'll try explaining why it preserves the order. 对于LINQ to Object ,我将尝试解释为什么它保留了订单。

In fact when the LINQ query is executed, the IEnumerable source will call to GetEnumerator() to start looping with a while loop and get the next element using MoveNext() . 实际上,当执行LINQ查询时, IEnumerable源将调用GetEnumerator()以使用while loop开始循环,并使用MoveNext()获取下一个元素。 This is how a foreach works on the IEnumerable source. 这是foreachIEnumerable源上的工作方式。 We all know that a foreach will preserve the order of the elements in a list/collection. 我们都知道foreach将保留列表/集合中元素的顺序 Digging more deeply into the MoveNext() , I think it just has some Position to save the current Index and MoveNext() just increase the Position and yield the corresponding element (at the new position). 深入挖掘MoveNext() ,我认为它只有一些Position来保存当前的IndexMoveNext()只是增加Positionyield相应的元素(在新位置)。 That's why it should preserve the order, all the code changing the original order is redundant or by explicitly calling to OrderBy or OrderByDescending . 这就是为什么它应该保留顺序, 所有改变原始顺序的代码都是冗余的,或者通过显式调用OrderByOrderByDescending

If you think this 如果你这么想的话

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 
foreach(var i in numbers)
  if(i < 5) Console.Write(i + "  ");

prints out 4 1 3 2 0 you should think this 打印4 1 3 2 0你应该这么想

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 
IEnumerator ie = numbers.GetEnumerator();
while(ie.MoveNext()){
  if((int)ie.Current < 5) Console.Write(ie.Current + "  ");
}

also prints out 4 1 3 2 0 . 还打印出4 1 3 2 0 Hence this LINQ query 因此这个LINQ查询

var lowNums = from n in numbers 
              where n < 5 
              select n;
foreach (var i in lowNums) { 
   Console.Write(i + "  "); 
}

should also print out 4 1 3 2 0 . 还应打印4 1 3 2 0

Conclusion: The order of elements in LINQ depends on how MoveNext() of an IEnumerator obtained from an IEnumerable is implemented. 结论: LINQ中元素的顺序取决于如何实现从IEnumerator获取的IEnumerable MoveNext() However, it's for sure that the order of elements in LINQ result will be the same order a foreach loop works on the elements . 但是,确保LINQ结果中元素的顺序与foreach循环在元素上的顺序相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM