要訂購Linq的清單,我們必須致電OrderBy
首先請撥打ThenBy
查詢下屬訂單的結果。訂單清單,頂級訂單未知
我現在處於一種我不知道頂級命令的情況。我有一份應有條件應用的排序清單。
像這樣:
var list = new List<Tuple<int, string, DateTime>>();
list.Add(new Tuple<int, string, DateTime>(1, "B", new DateTime(2020, 1, 1)));
list.Add(new Tuple<int, string, DateTime>(2, "A", new DateTime(2000, 1, 1)));
list.Add(new Tuple<int, string, DateTime>(3, "C", new DateTime(1900, 1, 1)));
var orderedList = list;
if (sortByString)
{
orderdedList = orderedList.ThenBy(listItem => listItem.Item2);
}
if (sortByDateTime)
{
orderedList = orderedList.ThenBy(listItem => listItem.Item3);
}
orderList = orderedList.ThenBy(listItem => listItem.Item1);
所以名單將始終由項目1的項目2和/或項目3第一排序,並根據條件。
如何在C#中完成此操作?沒有Linq的解決方案也是受歡迎的。
什麼地方錯了,你有什麼 - 只寫'無功orderedList = list.OrderBy(T => t.Item1);'和保留所有內容**但**最後一行 – Carsten
'ThenBy'僅在'IOrderedEnumerable'上可用,因此您可以使用類型檢查。如果'IOrderedEnumerable'使用'ThenBy'否則'OrderBy' – Jehof
@Carsten問題在於Johan想'Item1'排序* last *。這不是很容易閱讀,但這就是他寫的:) – Luaan