我一直在看下面的帖子,並試圖將其應用到礦山,但沒有運氣: LINQ Inner-Join vs Left-Join無法離開JOIN linq查詢工作!
我有以下查詢將返回0條記錄,每次我運行它:
var tasks = from tt in d.luProjectTaskTypes
join cbt in d.CostByTasks
on tt.ProjectTaskTypeID equals cbt.ProjectTaskTypeID into temp
from cbt in temp.DefaultIfEmpty()
where cbt.ProposalID == Convert.ToInt32(this.StateItems["PropNumber"]) || cbt.ProposalID == null
select new
{
ProposalId = (cbt.ProposalID == null ? Convert.ToInt32(this.StateItems["PropNumber"]) : cbt.ProposalID),
TaskId = tt.ProjectTaskTypeID,
CostByTaskId = (cbt.CostByTaskID == null ? 0 : cbt.CostByTaskID),
TypeOfWork = tt.ProjectTaskType,
AmountRequested = (cbt.AmountRequested == null ? 0 : cbt.AmountRequested),
CostShare = (cbt.CostShareAmount == null ? 0 : cbt.CostShareAmount)
};
其中luProjectTaskTypes
是具有選項列表的查找表。我希望爲此表中的每個條目返回一條記錄,無論它是否在CostByTasks
中匹配,但我總是得到0.我做錯了什麼?
UPDATE:
這是SQL是generating-
SELECT
(CASE
WHEN ([t1].[ProposalID]) IS NULL THEN @p1
ELSE [t1].[ProposalID]
END) AS [ProposalId], [t0].[ProjectTaskTypeID] AS [TaskId],
(CASE
WHEN ([t1].[CostByTaskID]) IS NULL THEN @p2
ELSE [t1].[CostByTaskID]
END) AS [CostByTaskId], [t0].[ProjectTaskType] AS [TypeOfWork],
(CASE
WHEN [t1].[AmountRequested] IS NULL THEN CONVERT(Decimal(33,4),@p3)
ELSE CONVERT(Decimal(33,4),[t1].[AmountRequested])
END) AS [AmountRequested],
(CASE
WHEN [t1].[CostShareAmount] IS NULL THEN CONVERT(Decimal(33,4),@p4)
ELSE CONVERT(Decimal(33,4),[t1].[CostShareAmount])
END) AS [CostShare]
FROM [frgprop].[luProjectTaskType] AS [t0]
LEFT OUTER JOIN [frgprop].[CostByTask] AS [t1] ON [t0].[ProjectTaskTypeID] = [t1].[ProjectTaskTypeID]
WHERE ([t1].[ProposalID] = @p0) OR (([t1].[ProposalID]) IS NULL)
生成的SQL看起來像什麼? – 2010-08-11 22:36:17
@Jon,SQL添加 – 2010-08-11 22:48:13
正如你所看到的,它肯定會產生一個LEFT JOIN ...所以我建議你開始對SQL本身進行黑客攻擊,直到產生你想要的東西,然後嘗試將它轉換回LINQ。 – 2010-08-11 22:55:34