0

I Know it is near duplicate. But I am looking for a little more solid answers.

I wondered always, is there any way to retain the order in LINQ To Objects. While searching for the solution, fortunately I found, Preserving order with LINQ. I have gone through it.

Also, I searched on MSDN sites, but there is no such remarks/words which confirms the word "Order Preservation".

My question is, however they will also loop through each and every element to process it. Sequential traversal will not be a overhead when compared to accessing the elements randomly.

  1. If not, Whether they have applied any sort of random algorithm to access the elements?

  2. Since, no word is specified about order in MSDN, which means, in future they can change implementation, so I should not ever trust LINQ methods regarding the order.

Is there any way to confirm the order of the retrieval.

Thanks.

NANDAKUMAR THANGAVELU
  • 611
  • 3
  • 15
  • 34
  • 1
    Retain the order for what? Almost all LINQ methods retain order. – SLaks Dec 13 '16 at 18:21
  • @SLaks: But no word is specified for the Order in the MSDN document right. Lets say, u want something in which it is exactly associated with the input order, then I have to know this right. I am frequently encountering these kind of things. So, willing to find the solution for it. – NANDAKUMAR THANGAVELU Dec 13 '16 at 18:24
  • When I wrote the answer to Preserving Order with LINQ, I checked the msdn reference for each method. For every method but Distinct, all methods are documented as preserving order in the way that my answer suggests. – Amy B Dec 14 '16 at 04:49
  • Hi @DavidB: Thanks for reply. I have cross checked a sample for the link, https://msdn.microsoft.com/en-us/library/bb342261(v=vs.110).aspx which is for ToList, but it says nothing about order.Could u pls mention the msdn link, where it is written,will be useful for reference. – NANDAKUMAR THANGAVELU Dec 14 '16 at 13:08
  • 1
    ToList is implemented by calling the constructor on List : https://msdn.microsoft.com/en-us/library/fkbw11z0(v=vs.110).aspx The documentation of the constructor says: The elements are copied onto the List in the same order they are read by the enumerator of the collection. You can see the implementation of ToList here: https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/ToCollection.cs – Amy B Dec 14 '16 at 18:32
  • ah. There it is.. Thanks @DavidB. Could u pls add this as reply, so I can upvote and mark as answer.. – NANDAKUMAR THANGAVELU Dec 15 '16 at 06:07

0 Answers0