我打算将以下查询转换为linQ
SELECT TOP 100 S.TxID,
ToEmail,
[Subject],
ProcessedDate,
[Status] = (CASE WHEN EXISTS (SELECT TxID FROM TxBounceTracking
WHERE TxID = S.TxID)
THEN 'Bounced'
WHEN EXISTS (SELECT TxID FROM TxOpenTracking
WHERE TxID = S.TxID)
THEN 'Opened'
ELSE 'Sent' END)
FROM TxSubmissions S
WHERE S.UserID = @UserID
AND ProcessedDate BETWEEN @StartDate AND @EndDate
ORDER BY ProcessedDate DESC
以下代码是我转换的linq。
v = (from a in dc.TxSubmissions
where a.ProcessedDate >= datefrom && a.ProcessedDate <= dateto && a.UserID == userId
let bounce = (from up in dc.TxBounceTrackings where up.TxID == a.TxID select up)
let track = (from up in dc.TxOpenTrackings where up.TxID == a.TxID select up)
select new { a.TxID, a.ToEmail, a.Subject,
Status = bounce.Count() > 0 ? "Bounced" : track.Count() > 0 ? "Opened" : "Sent",
a.ProcessedDate });
但是这个linq太慢了,因为跳转和跟踪表,我应该如何更改linq查询以选择一行只匹配上面的SQL查询&gt;&gt;
SELECT TxID FROM TxOpenTracking WHERE TxID = S.TxID
在我选择的列中,因此可以更快地执行。
请注意,该记录包含一百万条记录,这就是它滞后的原因
答案 0 :(得分:1)
由于您不会关心可读性,因为您最终会通过EF生成查询,因此您可以尝试加入这两个表。 (看起来TxID是FK或PK / FK)
有关JOIN与SUB-QUERY的更多信息,请访问:Join vs. sub-query
基本上你的SQL看起来像这样:
SELECT TOP 100 S.TxID, ToEmail, [Subject], ProcessedDate,
[Status] = (CASE WHEN BT.TxID IS NOT NULL
THEN 'Bounced'
WHEN OP.TxID IS NOT NULL
THEN 'Opened'
ELSE 'Sent' END)
FROM TxSubmissions S
LEFT JOIN TxBounceTracking BT ON S.TxID = BT.TxID
LEFT JOIN TxOpenTracking OP ON S.TxID = OP.TxID
WHERE S.UserID = @UserID
AND ProcessedDate BETWEEN @StartDate AND @EndDate
ORDER BY ProcessedDate DESC
然后,您可以尝试将其转换为LINQ,例如:
v = (from subs in dc.TxSubmissions.Where(sub => sub.ProcessedDate >= datefrom && sub.ProcessedDate <= dateto && sub.UserID == userId)
from bts in dc.TxBounceTrackings.Where(bt => bt.TxID == subs.TxID).DefaultIfEmpty()
from ots in dc.TxOpenTrackings.Where(ot => ot.TxID == subs.TxID).DefaultIfEmpty()
select new { });
更多关于linq的左连接:LEFT JOIN in LINQ to entities?
此外,如果您删除默认值,如果为空,您将获得内部联接。
在这两种情况下,您还需要查看生成的SQL。