我有一個如下所示的 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 概念和使用字串陣列執行 Contains 類方法時遇到困難。我還意識到日期的型別包含在一個可為空的 int 中。
感謝您到目前為止的所有建議~!
uj5u.com熱心網友回復:
嘗試將條件移動到結果中:
select new
{
Date = a.EndDate,
VisitType = allowedTypes.Contains(a.Type) ? "foo" : "bar",
VisitDuration = ???
}
uj5u.com熱心網友回復:
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、屬性 startDate 的值大于 startDate 的值以及 endDate 的值小于 endDate 的那些 Claims。
從剩余 Claims 序列中的每個 Claims 中,創建一個具有三個屬性的新物件。
- 日期是索賠的結束日期,
- 持續時間是索賠的結束日期 - 開始日期
- 如果 Claim 的 VisityType 在 allowedTypes 中,則屬性 VisitType 的值為“foo”,否則 VisitType 的值為“bar”
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377994.html
上一篇:手動排序,2×2?
