【问题标题】:IF ELSE condition in SELECT statement of LINQ query with DATEADD() logic带有 DATEADD() 逻辑的 LINQ 查询的 SELECT 语句中的 IF ELSE 条件
【发布时间】:2019-07-22 08:04:23
【问题描述】:

更新: 我正在尝试使用 LINQ 将以下 SQL 逻辑转换为 C# 代码,我对如何处理 SELECT 语句和 DATEADD 函数中的 CASE WHEN 和 ELSE 条件几乎没有混淆。

WITH tmp AS (
    SELECT  
    ID, 
    StartDate AS ReportingDate, 
    IncrementYears, 
    IncrementMonths,
     IncrementDays,
      DATEADD(YEAR, 1, GETDATE()) AS EndDate
    FROM    TransactionDetailDateFrequency
    UNION ALL
    SELECT  tmp.ID,
            CASE WHEN 
            tmp.IncrementDays > 0 
            THEN DATEADD(YEAR,tmp.IncrementYears,DATEADD(MONTH,tmp.IncrementMonths,DATEADD(DAY,tmp.IncrementDays,tmp.ReportingDate)))
            ELSE 
            EOMONTH(DATEADD(YEAR,tmp.IncrementYears,DATEADD(MONTH,tmp.IncrementMonths,tmp.ReportingDate))) 
            END AS ReportingDate,
            tmp.IncrementYears, 
            tmp.IncrementMonths, 
            tmp.IncrementDays, 
            tmp.EndDate
    FROM    tmp
    WHERE   tmp.ReportingDate < tmp.EndDate) 

    select * from tmp
    OPTION (MAXRECURSION 0)

到目前为止,我已经转换了它,

var calculatedDate = DateTime.Now.AddYears(1);

        var items = (from TDD in IngestionHubContext.TransactionDetailDateFrequency
                     select new { TDD.Id, ReportingDate = TDD.StartDate, TDD.IncrementYears, TDD.IncrementMonths, TDD.IncrementDays, EndDate = calculatedDate });

        var Temp = items.Concat(from T in items
                                where T.ReportingDate < T.EndDate
                                select new
                                {
                                    T.Id,
                                    ReportingDate = T.IncrementDays > 0 ? T.ReportingDate.Value.AddYears(T.IncrementYears.Value).AddMonths(T.IncrementMonths.Value).AddDays(T.IncrementDays.Value):
                                     T.ReportingDate.Value.AddYears(T.IncrementYears.Value).AddMonths(T.IncrementMonths.Value),
                                    T.IncrementYears,
                                    T.IncrementMonths,
                                    T.IncrementDays,
                                    T.EndDate
                                });

有人可以指导我如何在 LINQ 中的 c# 中实现 EOMONTH() 逻辑

更新:我在上面添加了 Linq 查询,但在 items.Concat 处出现错误 - '不包含 concat 的定义和最佳扩展方法重载'ParallelEnumerable.Concat'':已解决

还有在 C# 中获得等效 EOMONTH 的任何提示

感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: c# sql-server linq lambda entity-framework-core-2.1


    【解决方案1】:

    您想要做的是更容易理解如何使用稍微不同的语句格式来执行您的 CASE。你只需要使用ternary operator。使用DATEADD 函数需要一些本机函数built into Entity Framework >= 6.0

    (from TDD in IngestionHubContext.TransactionDetailDateFrequency
    select new { 
        TDD.Id, 
        CalculatedDate = TDD.IncrementDays > 0 ?
            DbFunctions.AddYears(
                DbFunctions.AddMonths(
                    DbFunctions.AddDays(TDD.ReportingDate, 
                                        TDD.IncrementDays), 
                    TDD.IncrementMonths), 
                TDD.IncrementYears) :
            DbFunctions.AddYears(
                DbFunctions.AddMonths(TDD.ReportingDate,
                                      TDD.IncrementMonths),
                TDD.IncrementYears),
        ReportingDate = TDD.StartDate, 
        TDD.IncrementYears, 
        TDD.IncrementMonths, 
        TDD.IncrementDays, 
        EndDate = calculatedDate 
    });
    

    【讨论】:

    • 我使用的是 EF core 2.2,并且 DBFunctions 不包含 AddYears()/AddMonths/AddDays 的定义,我还需要像 SQL 查询一样使用 Concat 来联合 ALL。
    • 抱歉,这是第一个出现的问题。您能否将标签添加到您的问题中以避免再次混淆?如果我能在 Core 中找到如何操作,我会在一分钟内回来。
    • 我查看了文档,但没有看到任何内容。你能试着做TDD.ReportingDate.AddDays(TDD.IncrementDays).AddMonths(TDD.IncrementMonths).AddYears(TDD.IncrementYears)吗?如果此时他们没有将其包含在与以前相同的类中,那么他们可能只是将内置的本机 C# 方法转换为 DateTime
    • 明确地说,它实际上被称为条件运算符。三元只是指任何带有 3 个操作数的运算符,并且它是目前唯一的。
    • 我已经用新查询更新了问题,但在 items.Concat 处出现错误
    猜你喜欢
    • 2017-08-19
    • 2019-08-15
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多