如何在orderby
时忽略名称属性中的Nullstudent.Students= student.student.OrderBy(s=> s.Name ?? null).ToList();
以上代码始终会在列表中返回list of students having Name = null as 1st element
并student with name 'system' in the end.
我想要ignore/exclude null in orderby
。 n ull should always come to end of the the list
答案 0 :(得分:4)
您可以制作条件OrderBy
:
student.Students= student.student
.OrderBy(s=> s.Name == null ? 1 : 0)
.ThenBy(s => s.Name)
.ToList();
首先将其拆分为两个组,即s.Name != null
项和s.Name == null
项。第二个排序条件是Name
本身。