从 linq 查询获取日期的星期

发布于 2024-08-01 13:04:16 字数 425 浏览 9 评论 0 原文

我试图从数据库中获取日期的星期并将其与 DateTimePicker 中用户选择的值进行比较。 我的用户可以选择一天,我编写了一个类,以 1 到 52 的形式给出当天的周数。我尝试选择 get LINQ 来将数据库中给定日期的周数与用户选择的周数进行比较用户。 示例:

var report = (from orders in Context.Orders
              where DateEx.ISOWeekNumber(orders.Date) == 
                    DateEx.ISOWeekNumber(datepicker1.Value)
              select orders);

但它根本不起作用,因为 LINQ 没有 ISOWeekNumber 的翻译。 有人知道我该怎么做吗?

I am trying to get the week of a date from the database and compare it to a user selected value from a DateTimePicker. My users can select a day and I wrote a class that gives me the week of that day in the form 1 to 52. I am trying to select get LINQ to compare the weeks of a given date from the database to the week selected by the user. Example:

var report = (from orders in Context.Orders
              where DateEx.ISOWeekNumber(orders.Date) == 
                    DateEx.ISOWeekNumber(datepicker1.Value)
              select orders);

But it is not working at all as LINQ doesn't have a translation for the ISOWeekNumber.
Anyone knows how I can do this?

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

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

发布评论

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

评论(3

姐不稀罕 2024-08-08 13:04:17

我使用了 System.Globalization 命名空间中的 Calendar.GetWeekOfYear 。

MSDN 链接

        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        DateTime specifiedDate = DateTime.Now;
        Calendar cal = dfi.Calendar;
        int weekNo = cal.GetWeekOfYear(specifiedDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
        var dataForWeek = data.Where(x => cal.GetWeekOfYear(x.DeliveryDate.Value, dfi.CalendarWeekRule, dfi.FirstDayOfWeek) == weekNo);

I used Calendar.GetWeekOfYear which is in the System.Globalization namespace.

MSDN Link

        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        DateTime specifiedDate = DateTime.Now;
        Calendar cal = dfi.Calendar;
        int weekNo = cal.GetWeekOfYear(specifiedDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
        var dataForWeek = data.Where(x => cal.GetWeekOfYear(x.DeliveryDate.Value, dfi.CalendarWeekRule, dfi.FirstDayOfWeek) == weekNo);
一笔一画续写前缘 2024-08-08 13:04:17

您可以编写一个完全满足您需要的 C# 方法。 LINQ 的优点之一是在编写查询时可以使用完整编程语言的所有工具。

在 Google 上快速搜索“isoweeknumber c#”,结果如下:

private int weekNumber(DateTime fromDate)
{
    // Get jan 1st of the year
    DateTime startOfYear = fromDate.AddDays(- fromDate.Day + 1).AddMonths(- fromDate.Month +1);
    // Get dec 31st of the year
    DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
    // ISO 8601 weeks start with Monday
    // The first week of a year includes the first Thursday
    // DayOfWeek returns 0 for sunday up to 6 for saterday
    int[] iso8601Correction = {6,7,8,9,10,4,5};
    int nds = fromDate.Subtract(startOfYear).Days  + iso8601Correction[(int)startOfYear.DayOfWeek];
    int wk = nds / 7;
    switch(wk)
    {
        case 0 :
            // Return weeknumber of dec 31st of the previous year
            return weekNumber(startOfYear.AddDays(-1));
        case 53 :
            // If dec 31st falls before thursday it is week 01 of next year
            if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
                return 1;
            else
                return wk;
        default : return wk;
    }
}

来源:http://codebetter.com/blogs/peter.van.ooijen/archive/2005/09/26/132466.aspx

You could write a C# method that does exactly what you need. One of the nice things about LINQ is how you have all the tools of a full programming language available to you when you write your queries.

A quick Google search for "isoweeknumber c#" turned up this:

private int weekNumber(DateTime fromDate)
{
    // Get jan 1st of the year
    DateTime startOfYear = fromDate.AddDays(- fromDate.Day + 1).AddMonths(- fromDate.Month +1);
    // Get dec 31st of the year
    DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
    // ISO 8601 weeks start with Monday
    // The first week of a year includes the first Thursday
    // DayOfWeek returns 0 for sunday up to 6 for saterday
    int[] iso8601Correction = {6,7,8,9,10,4,5};
    int nds = fromDate.Subtract(startOfYear).Days  + iso8601Correction[(int)startOfYear.DayOfWeek];
    int wk = nds / 7;
    switch(wk)
    {
        case 0 :
            // Return weeknumber of dec 31st of the previous year
            return weekNumber(startOfYear.AddDays(-1));
        case 53 :
            // If dec 31st falls before thursday it is week 01 of next year
            if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
                return 1;
            else
                return wk;
        default : return wk;
    }
}

Source: http://codebetter.com/blogs/peter.van.ooijen/archive/2005/09/26/132466.aspx

请恋爱 2024-08-08 13:04:17

类似的事情又如何呢?

Math.Truncate(date.DayOfYear/7) +1

给您:

var report = (from orders in Context.Orders
    where Math.Truncate(orders.Date.DayOfYear/7) + 1 ==
    Math.Truncate(datepicker1.Value.DayOfYear/7) + 1
    select orders);

希望有帮助,

Dan

可能会将 Math.Truncate(date.DayOfYear/7) 放入您的新 DateEx 类中

What about something of the ilk?

Math.Truncate(date.DayOfYear/7) +1

Giving you:

var report = (from orders in Context.Orders
    where Math.Truncate(orders.Date.DayOfYear/7) + 1 ==
    Math.Truncate(datepicker1.Value.DayOfYear/7) + 1
    select orders);

Hope that helps,

Dan

Probably take Math.Truncate(date.DayOfYear/7) into your new DateEx class

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