【问题标题】:LINQ OrderBy with more than one field and also with multiple conditions具有多个字段且具有多个条件的 LINQ OrderBy
【发布时间】:2016-03-15 10:22:22
【问题描述】:

我有一个需要按两个字段排序的列表。我正在寻找要按第一个字段排序的列表,然后,如果第一个字段中有任何空值,则按第二个字段排序。
我有两个字段,例如 Distancefrom time,我想要一个包含这两个字段的排序列表。但是我想优先考虑 from time 然后 distance 那么如何使用 Linq 对列表进行排序?

【问题讨论】:

标签: c# linq


【解决方案1】:

您可以像这样使用许多 orderBy 关闭:

from user in users
orderby user.Name, user.Age, user.Name.Length
select user;

或者你可以这样做:

OrderBy(<sort 1>).ThenBy(<sort 2>)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多