开发者

How to use Linq group a order list by Date

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)
 .Sel开发者_StackOverflow社区ect(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.


Use the Date property.

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


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

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


Use t => t.DateCreate.Date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜