已更新: 我正在尝试使用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查询,但在项上出现错误。concat-'不包含concat的定义和最佳扩展方法重载'ParallelEnumerable.Concat <>'':已解决
任何获得C#中的EOMONTH等效的技巧
感谢您的帮助。
谢谢。
答案 0 :(得分: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
});