C#将我的查询解释为内连接而不是左外连接,这种情况发生在什么情况下?

时间:2014-10-31 09:45:36

标签: c# linq linq-to-sql left-join linqpad

我的MVC应用程序中有两个不同的查询。两个查询都在LINQPAD中生成预期结果。但是我的一个查询不会产生左外连接结果;相反,它产生内部连接结果,其中左表中的空记录被消除并产生其他记录。

我的查询看起来像

var query = from inq in Inquiries
join sp in SalesPersons on inq.AssignedToID equals (int?)sp.SalesPersonID
into inquirySalesJoin
from sp in inquirySalesJoin.DefaultIfEmpty()
where (!inq.Deleted)
select new 
{
InquiryID = inq.InquiryID,
NameFirst = inq.NameFirst,
Salutation = inq.SalutationID,
NameLast = inq.NameLast,   
AssignedToID = inq.AssignedToID,
AssignedToDescription = (inq.AssignedToID == null ? "Not Assigned" : ((sp.NameFirst == null ? "" : sp.NameFirst) + " " + (sp.NameLast == null ? "" : sp.NameLast))),
InquiryStatus = inq.InquiryStatus
}

当我将我的查询转换为字符串时,生成的SQL语句不包含任何外连接方法,它实际上执行内连接

"SELECT 
[Project1].[InquiryID] AS [InquiryID], 
[Project1].[NameFirst] AS [NameFirst], 
[Project1].[SalutationID] AS [SalutationID], 
[Project1].[NameLast] AS [NameLast], 
[Project1].[CompanyName] AS [CompanyName], 
[Project1].[JobTitle] AS [JobTitle], 
[Project1].[TelePhone] AS [TelePhone], 
[Project1].[Email] AS [Email], 
[Project1].[InterestedProductID] AS [InterestedProductID], 
[Project1].[Others] AS [Others], 
[Project1].[ReferedBy] AS [ReferedBy], 
[Project1].[Comments] AS [Comments], 
[Project1].[AssignedToID] AS [AssignedToID], 
[Project1].[C1] AS [C1], 
[Project1].[InquiryStatus] AS [InquiryStatus]
FROM ( SELECT 
    [Extent1].[InquiryID] AS [InquiryID], 
    [Extent1].[NameLast] AS [NameLast], 
    [Extent1].[NameFirst] AS [NameFirst], 
    [Extent1].[CompanyName] AS [CompanyName], 
    [Extent1].[JobTitle] AS [JobTitle], 
    [Extent1].[TelePhone
] AS [TelePhone], 
    [Extent1].[Email] AS [Email], 
    [Extent1].[InterestedProductID] AS [InterestedProductID], 
    [Extent1].[Others] AS [Others], 
    [Extent1].[ReferedBy] AS [ReferedBy], 
    [Extent1].[Comments] AS [Comments], 
    [Extent1].[AssignedToID] AS [AssignedToID], 
    [Extent1].[InquiryStatus] AS [InquiryStatus], 
    [Extent1].[SalutationID] AS [SalutationID], 
    CASE WHEN ([Extent2].[NameFirst] IS NULL) THEN N'' ELSE [Extent2].[NameFirst] END + N' ' + CASE WHEN ([Extent2].[NameLast] IS NULL) THEN N'' ELSE [Extent2].[NameLast] END AS [C1]
    FROM  [DBO].[Inquiry] AS [Extent1]
    INNER JOIN [DBO].[SalesPerson] AS [Extent2] ON [Extent1].[AssignedToID] = [Extent2].[SalesPersonID]
    WHERE [Extent1].[Deleted] <> cast(1 as bit)
)  AS [Project1]
ORDER BY [Project1].[InquiryID] ASC"

我想知道这种情况发生了什么?

我的另一个查询我没有赋予nullable int?到FK id。

1 个答案:

答案 0 :(得分:0)

只需将代码更改为

即可
var query = from inq in Inquiries
        join sp in SalesPersons on inq.AssignedToID equals (int?)sp.SalesPersonID
        into inquirySalesJoin
        from subsp in inquirySalesJoin.DefaultIfEmpty()
        where (!inq.Deleted)
        select new 
        {
        InquiryID = subsp.InquiryID,
        NameFirst = subsp.NameFirst,
        Salutation = subsp.SalutationID,
        NameLast = subsp.NameLast,   
        AssignedToID = subsp.AssignedToID,
        AssignedToDescription = (subsp.AssignedToID == null ? "Not Assigned" : ((subsp.NameFirst    == null ? "" : subsp.NameFirst) + " " + (subsp.NameLast == null ? "" : subsp.NameLast))),
        InquiryStatus = subsp.InquiryStatus
        }