How to use Linq to group DateTime by month and days calculation
- by Daoming Yang
Hi all, I have two questions:
First one:
I have a order list and want to group them by the created month for the reports.
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"?
Note: the DateCreated is the DateTime tpye
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();
Second one: Output the full months between two dates in C#
If an user choose 2010-04-08 and 2010-06-04, I want to output the 2010-04-01 and 2010-06-30.
I can always get the first day and last day of the months, but I want to find out some other options
Many thanks.
Many thanks.