gpt4 book ai didi

c# - LINQ orderby 降序排列日期字段

转载 作者:IT王子 更新时间:2023-10-29 04:10:00 28 4
gpt4 key购买 nike

如何更改下面代码中的 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

最佳答案

env.OrderByDescending(x => x.ReportDate)

关于c# - LINQ orderby 降序排列日期字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com