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
你可以做一个条件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
本身。
归档时间: |
|
查看次数: |
2071 次 |
最近记录: |