Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/2996128/how-extension-methods-work-in-background/2996151#2996151">Others have explained</a> very well how extension methods are converted into static methods during compilation so I'm not going to get into that.</p> <p>I think what you are asking though is how OrderBy and ThenBy manage to produce the correct ordering when they are called in sequence, so I am going to try and answer that part of your question.</p> <hr> <p>The extension method <a href="http://msdn.microsoft.com/en-us/library/bb534966.aspx" rel="nofollow noreferrer">Enumerable.OrderBy</a> returns an <a href="http://msdn.microsoft.com/en-us/library/bb534852.aspx" rel="nofollow noreferrer">IOrderedEnumerable</a>, the concrete class that backs this is an OrderedEnumerable, which internally stores the function that is used to sort the enumerable.</p> <p>When you call ThenBy, you are calling the static <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.thenby.aspx" rel="nofollow noreferrer">Enumerable.ThenBy</a> method and passing the OrderedEnumerable from the output of the first call to OrderBy which creates a second OrderedEnumerable. This second OrderedEnumerable will contain a reference to the parent OrderedEnumerable that was created the first time.</p> <p>So what you have is a OrderedEnumerable that contains a parent OrderedEnumerable, each with the appropriate functions stored for ordering. When you enumerate over it, each OrderedEnumerable first delegates to it's parent, and only uses it's own sorting function when the parent is unable to separate the items being sorted. Obviously, there is no reason why you can't have a chain of several OrderedEnumerables, and it will always be the inner most that gets to call it's sorting function first.</p> <p>I drew a quick diagram to try and help explain:</p> <p><a href="http://i48.tinypic.com/smdf1g.png" rel="nofollow noreferrer">link text http://i48.tinypic.com/smdf1g.png</a></p> <hr> <p>I hope that makes sense. I hope I haven't confused the situation there, I think I've pretty much worn out the word 'enumerable'. If you need more detail you can use <a href="http://www.red-gate.com/products/reflector/" rel="nofollow noreferrer">reflector</a> to take a look at the what the various methods are doing under the covers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload