Linq to SQL 中按一年中的周(周数)分组

发布于 2024-09-12 21:43:31 字数 331 浏览 8 评论 0原文

在常规 SQL 中,我可以执行类似的操作:

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

如何在 Linq to SQL 中执行此操作?

以下不起作用

From F In DB.T Group R By DatePart(DateInterval.WeekOfYear, F.Date)

也不起作用:

From F In DB.T Group R By (F.Date.DayOfYear / 7)

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 following don't work

From F In DB.T Group R By DatePart(DateInterval.WeekOfYear, F.Date)

Also don't work:

From F In DB.T Group R By (F.Date.DayOfYear / 7)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉南桥 2024-09-19 21:43:34

LINQ to SQL 不支持 Calendar.WeekOfYear 方法,但您可能会创建一个 TSQL 包装对 DatePart 调用的函数 。 DayOfYear / 7 技巧应该适用于大多数情况,并且更容易使用。这是我最终得到的代码:

var x = from F in DB.T
        group F by new {Year = F.Date.Year, Week = Math.Floor((decimal)F.Date.DayOfYear / 7)} into FGroup
        orderby FGroup.Key.Year, FGroup.Key.Week
        select new {
            Year = FGroup.Key.Year,
            Week = FGroup.Key.Week,
            Count = FGroup.Count()
        };

结果如下:

Year    Week    Count
2004    46      3
2004    47      3
2004    48      3
2004    49      3
2004    50      2
2005    0       1
2005    1       8
2005    2       3
2005    3       1
2005    12      2
2005    13      2

LINQ to SQL does not support the Calendar.WeekOfYear method, but you could potentially create a TSQL function that wraps the call to DatePart. The DayOfYear / 7 trick should work for most cases and is much easier to use. Here's the code I ended up with:

var x = from F in DB.T
        group F by new {Year = F.Date.Year, Week = Math.Floor((decimal)F.Date.DayOfYear / 7)} into FGroup
        orderby FGroup.Key.Year, FGroup.Key.Week
        select new {
            Year = FGroup.Key.Year,
            Week = FGroup.Key.Week,
            Count = FGroup.Count()
        };

Results in something like this:

Year    Week    Count
2004    46      3
2004    47      3
2004    48      3
2004    49      3
2004    50      2
2005    0       1
2005    1       8
2005    2       3
2005    3       1
2005    12      2
2005    13      2
用心笑 2024-09-19 21:43:34

您可以使用 System.Data.Entity.SqlServer 命名空间。

// Return the week number
From F In DB.T Group R By SqlFunctions.DatePart("week", F.Date)

You can use the SqlFunctions.DatePart method from the System.Data.Entity.SqlServer namespace.

// Return the week number
From F In DB.T Group R By SqlFunctions.DatePart("week", F.Date)
一场春暖 2024-09-19 21:43:34

这工作正常。

from F in DB.T group F by F.Date.DayOfYear / 7;

您错误地指定了组。该代码的结果是对象的集合。每个对象都有一个 Key 属性,该属性将是您分组的依据(在本例中为 F.Date.DayOfYear / 7 的结果)。每个对象将是来自 T 的满足该组的对象的集合健康)状况。

This works correctly.

from F in DB.T group F by F.Date.DayOfYear / 7;

You were specifying the group by incorrectly. The result of this code be a collection of objects. Each object will have a Key property which will be what you grouped by (in this case the result of F.Date.DayOfYear / 7. Each object will be a collection of objects from T that met the group condition.

难如初 2024-09-19 21:43:34

如果您担心您所在的文化,以下代码将考虑到这一点:

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;

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;
情痴 2024-09-19 21:43:34

首先,您应该获取一周中第一天的日期。

获取一周中第一天的日期。
您可以使用此代码:

public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }
        return dt.AddDays(-1 * diff).Date;
    }
}

然后您可以按一周的第一个日期进行分组。

所以常规 SQL 中的这段代码:

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

可以在 Linq to SQL 中像这样完成

T.GroupBy(i => i.Date.StartOfWeek(DayOfWeek.Monday));

First you should get the date of the first day in the week.

To get the date of the first day in the week.
you can use this code:

public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }
        return dt.AddDays(-1 * diff).Date;
    }
}

Then you can group by the first date of the week.

So this code in regular SQL :

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

can be done in Linq to SQL like this

T.GroupBy(i => i.Date.StartOfWeek(DayOfWeek.Monday));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文