LINQ: Group by month and year within a datetime field

前端 未结 5 1151
轻奢々
轻奢々 2020-12-08 00:38

I have a table with a datetime field. I want to retrieve a result set grouped by the month/year combination and the number of records that appear within that month/year. H

5条回答
  •  萌比男神i
    2020-12-08 01:14

    you could also do it this way

    from o in yg
    group o by o.OrderDate.ToString("MMM yyyy") into mg
    select new { Month = mg.Key, Orders = mg }
    

    Your result will be

    {Jan 2014, 25} {Feb 2015, 15} etc...

提交回复
热议问题