【发布时间】:2011-04-28 04:00:25
【问题描述】:
如何更改下面代码中的 LINQ 查询以按日期降序排序(最新的在前,最早的在后)?
using System;
using System.Linq;
using System.Collections.Generic;
namespace Helloworld
{
class MainClass
{
public static void Main (string[] args)
{
List<Envelops> env = new List<Envelops> ();
Envelops e = new Envelops { ReportDate = DateTime.Now };
env.Add (e);
e = new Envelops { ReportDate = DateTime.Now.AddDays (5) };
env.Add (e);
e = new Envelops { ReportDate = new DateTime (2011, 3, 3) };
env.Add (e);
e = new Envelops { ReportDate = DateTime.Now };
env.Add (e);
foreach (Envelops r in env) {
Console.WriteLine ( r.ReportDate.ToString("yyyy-MMM"));
}
var ud = (from d in env
select d.ReportDate.ToString("yyyy-MMM") ).Distinct();
Console.WriteLine ("After distinct");
foreach (var r in ud) {
Console.WriteLine (r);
}
}
}
class Envelops
{
public DateTime ReportDate { get; set; }
}
}`enter code here`
当前输出为:
2011-Apr
2011-May
2011-Mar
2011-Apr
After distinct
2011-Apr
2011-May
2011-Mar
我希望按以下顺序输出:
may
april
march order
【问题讨论】: