How to use the Linq group order list by date

I have a list of orders and want to group them by creation date.

Each datetime order created will look like "2010-03-13 11:17: 16.000"

How can I make them only a group by date , for example, "2010-03-13"?

 var items = orderList.GroupBy(t => t.DateCreated)
 .Select(g => new Order()
 {
     DateCreated = g.Key

 })
 .OrderByDescending(x => x.OrderID).ToList();

Update: how can I sort by month? And the following code is incorrect.

var items = orderList.GroupBy(t => t.DateCreated.Month)
 .Select(g => new Order()
 {
     DateCreated = g.Key

 })
 .OrderByDescending(x => x.OrderID).ToList();

Many thanks.

+3
source share
3 answers

Use the Date property.

var items = orderList.GroupBy( t => t.DateCreated.Date )
+2
source

Usage .Datewill discard the time component e.g.

var items = orderList.GroupBy(t => t.DateCreated.Date)
+2
source

Use t => t.DateCreate.Date.

0
source

Source: https://habr.com/ru/post/1740438/


All Articles