【发布时间】:2023-03-06 01:35:01
【问题描述】:
我有一个如下所示的 SQL 查询:
SELECT
a.Date,
CASE
WHEN a.Type = 'String 1' OR a.Type = 'String 2'
THEN 'foo'
ELSE 'bar'
END AS VisitType,
DATEDIFF (d, (SELECT TOP 1 Date FROM dim.Date WHERE DateKey = a.StartDate),
(SELECT TOP 1 Date FROM dim.Date WHERE DateKey = a.EndDate)) AS Duration
我正在尝试将其转换为 C# 表达式,到目前为止我有这样的东西:
var allowedTypes = new[]{"String 1","String 2", "String 3", "String 4"}
var Data = from a in dbContext.Claim
where a.MemberId = memberId
&& a.StartDate > startDate
&& a.EndDate <= endDate
&& a.Type.Where(???t => allowedTypes.Contains(allowedTypes)) // This line I have issues with
select new
{
Date = a.EndDate,
VisitType = ???,
VisitDuration = ???
}
我在使用 DateDiff 概念时遇到困难,并且在使用字符串数组执行包含类似方法时遇到了困难。 我也意识到日期的类型包含在一个可为空的 int 中。
感谢您迄今为止的所有建议~!
【问题讨论】:
-
.First() 或 .FirstOrDefault() 是替换 SELECT TOP 1 的好考虑
-
可空整数中的日期?您的数据库服务器可能会尝试这样做,但实体框架肯定不会。不要将日期存储为 int,这只是糟糕的设计。
-
我认为
Claim.StartDate和.EndDate是日期表的外键???为什么不直接将日期值放在表格中? -
@JeremyLakeman 那完全是另外一罐蠕虫。哈哈
标签: c# entity-framework linq