两个IQueryables的组合不起作用

时间:2016-10-31 12:51:12

标签: sql asp.net

我正在开发一个ASP.NET应用程序,其中包含两个选择字段的页面。 一个用于搜索所有人到选定的部门,另一个用于搜索具有给定名称的人(或者用LIKE搜索其中一部分)。 对于每个选择字段,我得到一个查询,该查询获得相同框架实体的IQueryable对象。 但是当我尝试将IQueryable对象及其结果与Intersect(由于AND-Logic)结合起来时,只处理第一个查询而第二个查询不处理。 我做错了什么? 也许我的查询中有错误?

以下是查询:

第一个搜索人员到选定的部门。

{SELECT 
    [Extent1].[PersonID] AS [PersonID], 
    CASE WHEN (([Extent1].[InstanceID] IS NOT NULL) AND ([Extent1].[InstanceID] IS NOT NULL)) THEN [Extent1].[InstanceID] ELSE 0 END AS [C1], 
    CASE WHEN (([Extent1].[bHidden] IS NOT NULL) AND ([Extent1].[bHidden] IS NOT NULL)) THEN [Extent1].[bHidden] ELSE cast(0 as bit) END AS [C2], 
    [Extent1].[txtLastName] AS [txtLastName], 
    [Extent1].[txtFirstName] AS [txtFirstName], 
    [Extent1].[txtEMail] AS [txtEMail], 
    [Extent1].[txtAlternativeEMail] AS [txtAlternativeEMail], 
    [Extent1].[txtGID] AS [txtGID], 
    [Extent1].[txtPhoneNumber] AS [txtPhoneNumber], 
    CASE WHEN (([Extent1].[iImportance] IS NOT NULL) AND ([Extent1].[iImportance] IS NOT NULL)) THEN [Extent1].[iImportance] ELSE 0 END AS [C3], 
    [Extent1].[Teamlead] AS [Teamlead]
    FROM (SELECT 
    [Person_Teamlead].[PersonID] AS [PersonID], 
    [Person_Teamlead].[InstanceID] AS [InstanceID], 
    [Person_Teamlead].[WiW_URL_PersonID] AS [WiW_URL_PersonID], 
    [Person_Teamlead].[txtAccount] AS [txtAccount], 
    [Person_Teamlead].[txtTitle] AS [txtTitle], 
    [Person_Teamlead].[txtLastName] AS [txtLastName], 
    [Person_Teamlead].[txtFirstName] AS [txtFirstName], 
    [Person_Teamlead].[txtPhoneNumber] AS [txtPhoneNumber], 
    [Person_Teamlead].[txtEMail] AS [txtEMail], 
    [Person_Teamlead].[txtGID] AS [txtGID], 
    [Person_Teamlead].[txtDomain] AS [txtDomain], 
    [Person_Teamlead].[txtFax] AS [txtFax], 
    [Person_Teamlead].[txtMobilePhone] AS [txtMobilePhone], 
    [Person_Teamlead].[txtRoom] AS [txtRoom], 
    [Person_Teamlead].[txtAddress] AS [txtAddress], 
    [Person_Teamlead].[dtLastUpdate] AS [dtLastUpdate], 
    [Person_Teamlead].[bHidden] AS [bHidden], 
    [Person_Teamlead].[iExtern] AS [iExtern], 
    [Person_Teamlead].[txtOrganisationUnit] AS [txtOrganisationUnit], 
    [Person_Teamlead].[txtStreet] AS [txtStreet], 
    [Person_Teamlead].[txtZip] AS [txtZip], 
    [Person_Teamlead].[txtLocalityName] AS [txtLocalityName], 
    [Person_Teamlead].[txtCountry] AS [txtCountry], 
    [Person_Teamlead].[iToDelete] AS [iToDelete], 
    [Person_Teamlead].[dtToDelete] AS [dtToDelete], 
    [Person_Teamlead].[iImportance] AS [iImportance], 
    [Person_Teamlead].[iManualInput] AS [iManualInput], 
    [Person_Teamlead].[txtAlternativeEMail] AS [txtAlternativeEMail], 
    [Person_Teamlead].[Teamlead] AS [Teamlead]
    FROM [dbo].[Person_Teamlead] AS [Person_Teamlead]) AS [Extent1]
    WHERE ( EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Person_Department] AS [Extent2]
        WHERE ([Extent2].[DepartmentID] = @p__linq__0) AND ([Extent2].[PersonID] = [Extent1].[PersonID])
    )) AND ([Extent1].[InstanceID] = @p__linq__1) AND (0 = [Extent1].[iToDelete]) AND ((0 = (CASE WHEN ([Extent1].[bHidden] IS NOT NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END)) OR (0 = [Extent1].[bHidden]))}

第二个查询用于搜索给定姓名的人。

{SELECT 
    [Project1].[PersonID] AS [PersonID], 
    [Project1].[C1] AS [C1], 
    [Project1].[C2] AS [C2], 
    [Project1].[txtLastName] AS [txtLastName], 
    [Project1].[txtFirstName] AS [txtFirstName], 
    [Project1].[txtEMail] AS [txtEMail], 
    [Project1].[txtAlternativeEMail] AS [txtAlternativeEMail], 
    [Project1].[txtGID] AS [txtGID], 
    [Project1].[txtPhoneNumber] AS [txtPhoneNumber], 
    [Project1].[C3] AS [C3], 
    [Project1].[Teamlead] AS [Teamlead]
    FROM ( SELECT 
        [Extent1].[PersonID] AS [PersonID], 
        [Extent1].[txtLastName] AS [txtLastName], 
        [Extent1].[txtFirstName] AS [txtFirstName], 
        [Extent1].[txtPhoneNumber] AS [txtPhoneNumber], 
        [Extent1].[txtEMail] AS [txtEMail], 
        [Extent1].[txtGID] AS [txtGID], 
        [Extent1].[txtAlternativeEMail] AS [txtAlternativeEMail], 
        [Extent1].[Teamlead] AS [Teamlead], 
        CASE WHEN (([Extent1].[InstanceID] IS NOT NULL) AND ([Extent1].[InstanceID] IS NOT NULL)) THEN [Extent1].[InstanceID] ELSE 0 END AS [C1], 
        CASE WHEN (([Extent1].[bHidden] IS NOT NULL) AND ([Extent1].[bHidden] IS NOT NULL)) THEN [Extent1].[bHidden] ELSE cast(0 as bit) END AS [C2], 
        CASE WHEN (([Extent1].[iImportance] IS NOT NULL) AND ([Extent1].[iImportance] IS NOT NULL)) THEN [Extent1].[iImportance] ELSE 0 END AS [C3]
        FROM (SELECT 
    [Person_Teamlead].[PersonID] AS [PersonID], 
    [Person_Teamlead].[InstanceID] AS [InstanceID], 
    [Person_Teamlead].[WiW_URL_PersonID] AS [WiW_URL_PersonID], 
    [Person_Teamlead].[txtAccount] AS [txtAccount], 
    [Person_Teamlead].[txtTitle] AS [txtTitle], 
    [Person_Teamlead].[txtLastName] AS [txtLastName], 
    [Person_Teamlead].[txtFirstName] AS [txtFirstName], 
    [Person_Teamlead].[txtPhoneNumber] AS [txtPhoneNumber], 
    [Person_Teamlead].[txtEMail] AS [txtEMail], 
    [Person_Teamlead].[txtGID] AS [txtGID], 
    [Person_Teamlead].[txtDomain] AS [txtDomain], 
    [Person_Teamlead].[txtFax] AS [txtFax], 
    [Person_Teamlead].[txtMobilePhone] AS [txtMobilePhone], 
    [Person_Teamlead].[txtRoom] AS [txtRoom], 
    [Person_Teamlead].[txtAddress] AS [txtAddress], 
    [Person_Teamlead].[dtLastUpdate] AS [dtLastUpdate], 
    [Person_Teamlead].[bHidden] AS [bHidden], 
    [Person_Teamlead].[iExtern] AS [iExtern], 
    [Person_Teamlead].[txtOrganisationUnit] AS [txtOrganisationUnit], 
    [Person_Teamlead].[txtStreet] AS [txtStreet], 
    [Person_Teamlead].[txtZip] AS [txtZip], 
    [Person_Teamlead].[txtLocalityName] AS [txtLocalityName], 
    [Person_Teamlead].[txtCountry] AS [txtCountry], 
    [Person_Teamlead].[iToDelete] AS [iToDelete], 
    [Person_Teamlead].[dtToDelete] AS [dtToDelete], 
    [Person_Teamlead].[iImportance] AS [iImportance], 
    [Person_Teamlead].[iManualInput] AS [iManualInput], 
    [Person_Teamlead].[txtAlternativeEMail] AS [txtAlternativeEMail], 
    [Person_Teamlead].[Teamlead] AS [Teamlead]
    FROM [dbo].[Person_Teamlead] AS [Person_Teamlead]) AS [Extent1]
        WHERE ([Extent1].[InstanceID] = @p__linq__0) AND (0 = [Extent1].[iToDelete]) AND ((0 = (CASE WHEN ([Extent1].[bHidden] IS NOT NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END)) OR (0 = [Extent1].[bHidden])) AND ([Extent1].[txtLastName] LIKE @p__linq__1 ESCAPE N'~')
    )  AS [Project1]
    ORDER BY [Project1].[Teamlead] DESC, [Project1].[C3] DESC, [Project1].[txtLastName] ASC, [Project1].[txtFirstName] ASC}

1 个答案:

答案 0 :(得分:0)

我找到了它。 小事,大结果! 我忘了将Intersect的结果设置为原始查询。 我做了

query1.Intersect(query2);

而不是

query1 = query1.Intersect(query2);

这就是为什么只是始终执行query1。