Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ orderby on date field in descending order

Tags:

c#

linq

How can I change the LINQ query in the code below to sort by date in descending order (latest first, earliest last)?

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` 

the current output is:

2011-Apr 2011-May 2011-Mar 2011-Apr After distinct 2011-Apr 2011-May 2011-Mar 

I want the output to in the following order:

may april march order 
like image 485
sfgroups Avatar asked Apr 28 '11 04:04

sfgroups


People also ask

How do I get data in descending order in LINQ?

If you want to rearrange or sort the elements of the given sequence or collection in descending order in query syntax, then use descending keyword as shown in below example. And in method syntax, use OrderByDescending () method to sort the elements of the given sequence or collection.

Is LINQ OrderBy ascending?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

How to sort LINQ?

LINQ includes five sorting operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse. LINQ query syntax does not support OrderByDescending, ThenBy, ThenByDescending and Reverse. It only supports 'Order By' clause with 'ascending' and 'descending' sorting direction.


2 Answers

env.OrderByDescending(x => x.ReportDate) 
like image 118
Max Avatar answered Sep 19 '22 09:09

Max


I don't believe that Distinct() is guaranteed to maintain the order of the set.

Try pulling out an anonymous type first and distinct/sort on that before you convert to string:

var ud = 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); 
like image 44
Matt Hamilton Avatar answered Sep 20 '22 09:09

Matt Hamilton