gpt4 book ai didi

.net - 无法让 Left JOIN linq 查询工作!

转载 作者:行者123 更新时间:2023-12-01 01:33:21 30 4
gpt4 key购买 nike

我一直在查看以下帖子并尝试将其应用于我的但没有运气:
LINQ Inner-Join vs Left-Join

我有下面的查询,每次运行时都会返回 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。我做错了什么?!

更新:

这是它正在生成的 SQL-
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)

最佳答案

您的问题出在 WHERE 子句中

当您对表进行 LEFT JOIN 时,这很好,但是如果您在 LEFT-JOINED 表上设置条件,它基本上会将其变成 INNER JOIN。你应该允许 Nulls 过去。

where cbt.ProposalID == Convert.ToInt32(this.StateItems["PropNumber"]) OR cbt.ProposalID is NULL

我不确定 is NULL 语法如何——也许必须是 db.null(field)等——你必须检查一下;但这个概念是有效的。

关于.net - 无法让 Left JOIN linq 查询工作!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3463337/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com