要订购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