tags:

views:

57

answers:

3

I have a order list and want to group them by the created date.

Each order's created datetime will be like "2010-03-13 11:17:16.000"

How can I make them only group by date like "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 group order by month? And the following code is not correct.

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

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

Many thanks.

+1  A: 

Use the Date property.

var items = orderList.GroupBy( t => t.DateCreated.Date )
tvanfosson
+1  A: 

Using the .Date will discard the time component, e.g.

var items = orderList.GroupBy(t => t.DateCreated.Date)
Greg Beech
A: 

Use t => t.DateCreate.Date.

Marcelo Cantos