【发布时间】: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