[英]How to translate SQL to C#
我有一个看起来像这样的 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 中。
感谢您迄今为止的所有建议~!
尝试将条件移动到结果中:
select new
{
Date = a.EndDate,
VisitType = allowedTypes.Contains(a.Type) ? "foo" : "bar",
VisitDuration = ???
}
var result = dbContext.Claims
.Where (claim => claim.MemberId = memberId
&& claim.StartDate > startDate
&& claim.EndDate <= endDate
.Select(claim => new
{
Date = claim.EndDate,
VisitType = allowedTypes.Contains(claim.Type) ? "foo" : "bar",
VisitDuration = claim.EndDate - claim.StartDate,
});
换句话说:给定memberId
、 startDate
、 endDate
的值。 从 Claims 表中,仅保留属性 MemberId 的值等于 memberId、Property startDate 的值高于 startDate 和 endDate 的值小于 endDate 的那些 Claims。
从剩余声明序列中的每个声明中,创建一个具有三个属性的新 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.