gpt4 book ai didi

c# - LINQ-to-SQL - 在日期对象中按周分组

转载 作者:行者123 更新时间:2023-11-30 22:34:26 25 4
gpt4 key购买 nike

我正在用 LINQ-to-SQL 编写一个查询,这个查询中有一个日期列。我想按“周”对结果进行分组。我将如何在 LINQ-to-SQL 中执行此操作?

谢谢

最佳答案

假设公历,“第一天”周规则,一周的第一天是星期一。

首日周规则:

Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week

int firstDayOfWeek = (int)DayOfWeek.Monday;
var q =
from u in TblUsers
let date = u.CreateDate.Value
let num = date.DayOfYear - 1
let num2 = ((int)date.DayOfWeek) - (num % 7)
let num3 = ((num2 - firstDayOfWeek) + 14) % 7
let week = (((num + num3) / 7) + 1)
group u by week into g
select new
{
Week = g.Key,
Count = g.Count ()
};

关于c# - LINQ-to-SQL - 在日期对象中按周分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com