【问题标题】:Using LINQ to transform Date into a WeekDay and Week of Month使用 LINQ 将 Date 转换为 WeekDay 和 Week of Month
【发布时间】:2021-01-14 10:49:49
【问题描述】:

我正在尝试将此 MySQL 选择查询日期函数转换为 LINQ。但我不知道 WEEKDAY 或 DayOfMONth 的 LINQ 版本。下面的工作SQL。我找到了 DayOfWeek,但无法理解它的作用或如何将它用于我想要实现的目标。任何帮助表示赞赏,谢谢

Day = WEEKDAY(JS.Date),
Week = FLOOR((DayOfMonth(JS.Date)-1)/7)+1

这里有完整代码

var rows = from JS in _context.JobSales 

        join Msg in _context.Commenting.Where(a => a.ReplyTo == 0  && a.ToolSection == 2) on
            JS.Job equals Msg.ToolId
            into cmt from Msg in cmt.DefaultIfEmpty()

        orderby JS.Date descending, JS.Job
        where shopId_Array.Contains((char)JS.ShopId)
        && id == JS.ShopId
        && year == JS.Year
        && month == JS.Month
        select new {
            total = JS.RemoveRefit + JS.Repair + JS.Paint + JS.Parts + JS.SubMisc + JS.Other + JS.Mechanical + JS.Electrical,
            Day = WEEKDAY(JS.Date),
            Week = FLOOR((DayOfMonth(JS.Date)-1)/7)+1
        };

        //Return Results
        return rows;

【问题讨论】:

    标签: c# linq linq-to-sql


    【解决方案1】:

    您尝试过 SqlFunctions.DatePart 吗?

    代码看起来像这样,我还没有测试过。

    var weekday = _myTableRepository
    .Select(x => SqlFunctions.DatePart("weekday",x.CreationDate));
    

    【讨论】:

    • 没有。它可以同时处理这两个事情吗?
    • 我以前使用过 DatePart 并且它有效,但我不确定地板。当我找到东西时会通知你
    • 干杯 Sakkie 感谢您的帮助。它用于根据周一至周五推送每月的 # 周
    • 我很高兴迈克尔
    • 嗨 Sakkie,有没有一种特殊的技术可以将 SQLFunctions 引入到 C# 文件中?
    猜你喜欢
    • 2021-07-22
    • 2014-10-12
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多