C# 带有DATEADD()逻辑的LINQ查询的SELECT语句中的IF ELSE条件
更新: 我正在尝试使用LINQ将下面的SQL逻辑转换为C代码,对于如何处理SELECT语句和DATEADD函数中的CASE-WHEN和ELSE条件,我几乎没有什么疑惑C# 带有DATEADD()逻辑的LINQ查询的SELECT语句中的IF ELSE条件,c#,sql-server,linq,lambda,entity-framework-core-2.1,C#,Sql Server,Linq,Lambda,Entity Framework Core 2.1,更新: 我正在尝试使用LINQ将下面的SQL逻辑转换为C代码,对于如何处理SELECT语句和DATEADD函数中的CASE-WHEN和ELSE条件,我几乎没有什么疑惑 WITH tmp AS ( SELECT ID, StartDate AS ReportingDate, IncrementYears, IncrementMonths, IncrementDays, DATEADD(YEAR, 1, GETDATE()) A
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实现EOMOUNT逻辑吗
更新:我在上面添加了Linq查询,但在items处出现错误。Concat-“不包含Concat的定义和最佳扩展方法重载”ParallelEnumerable.Concat:已解决
还有什么技巧可以在C中获得EOMONTH等价物吗
感谢您的帮助
提前感谢。您想要做的是更容易理解如何使用稍微不同的语句格式执行您的案例。你只需要使用一个。使用DATEADD函数需要一些本机函数
我使用的是EF core 2.2,DBFunctions不包含AddYears/AddMonths/AddDays的定义。我还需要使用Concat来联合所有SQL查询。抱歉,这是第一个出现的问题。请在你的问题上加上标签,以避免再次出现这种混淆?如果我能找到如何在Core中实现它,我马上就回来。我浏览了文档,但没有看到任何内容。你能试着只做TDD.ReportingDate.AddDaysTDD.IncrementDays.addmonthsdd.incrementmonds.AddYearsTDD.IncrementYears吗?如果到目前为止,它们没有像以前一样将其包含在同一个类中,那么它们可能只是在翻译DateTime中内置的本机C方法。更清楚的是,它实际上被称为条件运算符。三元运算符只是指任何接受3个操作数的运算符,它是目前唯一一个接受3个操作数的运算符。我已使用新查询更新了问题,但在items.Concat处出现错误
(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
});