如何在order by子句中忽略'null'

时间:2016-03-07 11:50:43

标签: linq

如何在orderby

时忽略名称属性中的Null
student.Students= student.student.OrderBy(s=> s.Name ?? null).ToList();

以上代码始终会在列表中返回list of students having Name = null as 1st elementstudent with name 'system' in the end.

我想要ignore/exclude null in orderby。 n ull should always come to end of the the list

1 个答案:

答案 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本身。

相关问题