我通過101個的LINQ教程從這裏編碼:LINQ:單獨排序依據和thenby聲明
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
大多數例子很簡單,但是這一次扔我一個循環:
[Category("Ordering Operators")]
[Description("The first query in this sample uses method syntax to call OrderBy and ThenBy with a custom comparer to " +
"sort first by word length and then by a case-insensitive sort of the words in an array. " +
"The second two queries show another way to perform the same task.")]
public void Linq36()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry", "b1" };
var sortedWords =
words.OrderBy(a => a.Length)
.ThenBy(a => a, new CaseInsensitiveComparer());
// Another way. TODO is this use of ThenBy correct? It seems to work on this sample array.
var sortedWords2 =
from word in words
orderby word.Length
select word;
var sortedWords3 = sortedWords2.ThenBy(a => a, new CaseInsensitiveComparer());
無論我把它放在哪個單詞的哪個組合長度始終是第一個排序標準......即使我不知道第二個語句(沒有orderby!)知道原來的order by子句是什麼。
我會瘋了嗎?任何人都可以解釋Linq如何「記住」原始排序是什麼?
你可能想重新提出你的問題。我不確定你在這裏問什麼。 –
@JustinNiessner它對我來說非常有意義...... – Servy
它「記住」,因爲在這兩種情況下,這正是你告訴它做的。選擇,訂購,然後。我會說你會瘋了。 –