Group by Week of year (Week number) in Linq to SQL

后端 未结 6 1984
旧时难觅i
旧时难觅i 2021-01-05 17:20

In regular SQL i could do something like

SELECT * From T GROUP BY DATEPART(wk, T.Date)

How can i do that in Linq to SQL ?

The follo

6条回答
  •  借酒劲吻你
    2021-01-05 17:52

    If you are concerned about the culture you are in the following code will take that into account:

    var ci = CultureInfo.CurrentCulture;
    var cal = ci.Calendar;
    var rule = ci.DateTimeFormat.CalendarWeekRule;
    var firstDayOfWeek = ci.DateTimeFormat.FirstDayOfWeek;
    
    var groups = from F in DB.T
                 group F by cal.GetWeekOfYear(F, rule, firstDayOfWeek) into R
                 select R;
    

提交回复
热议问题