如何使用OrderBy对按日期升序的自定义对象进行排序

时间:2015-07-07 22:26:54

标签: c# linq sorting

我有一个自定义对象的集合,如下所示:

public struct Record {
    public string Caller { get; set; }
    public DateTime Started { get; set; }
    public string Dialed { get; set; }
    public decimal Duration { get; set; }
    public decimal Cost { get; set; }
    public string Location { get; set; }
    public Switch Switch { get; set; }
}

public static List<Record> Records { get; set; }

使用LINQ,如何使用Records中存储的值按升序对Record.Started进行排序?

2 个答案:

答案 0 :(得分:2)

只需使用OrderBy方法:

Records.OrderBy(record => record.Started)

或:

from r in Records
order by r.Started
select r

答案 1 :(得分:1)

难道这很容易吗?

records.OrderBy(r=>r.Started)