当前位置: 首页 > 编程语言 > C#

LINQorderby在日期字段中按降序排列分享

时间:2023-04-10 13:50:04 C#

LINQorderby在日期字段中按降序排列?使用系统;使用System.Linq;使用System.Collections.Generic;namespaceHelloworld{classMainClass{publicstaticvoidMain(string[]args){Listenv=newList();信封e=新信封{ReportDate=DateTime。现在};env.Add(e);e=newEnvelops{ReportDate=DateTime.Now.AddDays(5)};env.Add(e);e=newEnvelops{ReportDate=newDateTime(2011,3,3)};env.Add(e);e=新信封{ReportDate=DateTime.Now};env.Add(e);foreach(Envelopsrinenv){Console.WriteLine(r.ReportDate.ToString("yyyy-MMM"));}varud=(fromdinenvselectd.ReportDate.ToString("yyyy-MMM")).Distinct();Console.WriteLine("不同之后");foreach(varrinud){Console.WriteLine(r);}}}classEnvelops{publicDateTimeReportDate{get;放;}}}`entercodehere`当前输出是:2011-Apr2011-May2011-Mar2011-AprAfterdistinct2011-April2011-May2011-Mar我想按以下顺序输出:mayaprilmarchorder我不相信Distinct()可以保证保持集合的顺序首先尝试拉出一个匿名类型并对其进行区分/排序在转换为字符串之前:varud=env.Select(d=>new{d.ReportDate.Year,d.ReportDate.Month,FormattedDate=d.ReportDate.ToString("yyyy-MMM")}).Distinct()。OrderByDescending(d=>d.Year).ThenByDescending(d=>d.Month).Select(d=>d.FormattedDate);env.OrderByDescending(x=>x.ReportDate)这句话一定能帮到你:env=env.OrderByDescending(c=>c.ReportDate).ToList();以上是C#学习教程:LINQorderby将所有分享的内容在date字段中按降序排列。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关照——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: