如何忽略 order by 子句中的“null”

sim*_*ada 4 linq

orderby 时如何忽略 name 属性中的 Null

student.Students= student.student.OrderBy(s=> s.Name ?? null).ToList();
Run Code Online (Sandbox Code Playgroud)

上面的代码总是list of students having Name = null as 1st element在列表中返回并且student with name 'system' in the end.

我想要什么ignore/exclude null in orderby。然后ull should always come to end of the the list

Tim*_*ter 5

你可以做一个条件OrderBy

student.Students= student.student
    .OrderBy(s=> s.Name == null ? 1 : 0)
    .ThenBy(s => s.Name)
    .ToList();
Run Code Online (Sandbox Code Playgroud)

这首先分为两组,带有 的项目s.Name != null和带有 的项目s.Name == null。第二个排序条件是其Name本身。