LINQ查询的结果不等于SQL查询的结果

本文关键字:结果 查询 SQL 不等于 LINQ | 更新日期: 2023-09-27 17:53:22

我必须在几个实体(表)上创建一个SELECT,这需要大量的join。

当我在SQL Server Management Studio中编写该查询时,它有效,但在我的c#代码中使用LINQ,我没有得到匹配。

下面是一些例子:

SELECT 
    dbo.Person.PersonID,
    dbo.Person.txtFirstName,
    dbo.Person.txtLastName,
    dbo.Person.txtAccount
FROM
    [dbo].[Person]
INNER JOIN 
    dbo.CustomFieldData_Person ON dbo.Person.PersonID = dbo.CustomFieldData_Person.PersonID
INNER JOIN 
    dbo.CustomFieldData ON dbo.CustomFieldData_Person.CustomFieldDataID = dbo.CustomFieldData.CustomFieldDataID
INNER JOIN 
    dbo.CustomFieldListData ON dbo.CustomFieldData.CustomFieldDataID = dbo.CustomFieldListData.CustomFieldDataID
INNER JOIN 
    dbo.CustomListItem ON dbo.CustomFieldListData.CustomListItemID = dbo.CustomListItem.CustomListItemID
WHERE 
    [dbo].[CustomListItem].[CustomListItemID] = 218
    AND [dbo].[CustomListItem].[CustomListID] = 41
    AND [dbo].[Person].[InstanceID] = 80

匹配:30

List<Person> result =
        (from p in DB_Instance_Singleton.getInstance.Person
         join cfdp in DB_Instance_Singleton.getInstance.CustomFieldData_Person on p.PersonID equals cfdp.PersonID
         join cfd in DB_Instance_Singleton.getInstance.CustomFieldData on cfdp.CustomFieldDataID equals cfd.CustomFieldDataID
         join cfld in DB_Instance_Singleton.getInstance.CustomFieldListData on cfd.CustomFieldDataID equals cfld.CustomFieldDataID
         join cli in DB_Instance_Singleton.getInstance.CustomListItem on cfld.CustomListItemID equals cli.CustomListItemID
         where (cli.CustomListItemID == iCustomListItemID)
         && (cli.CustomListID == iCustomListID)
         && (p.InstanceID == iInstanceID)
         select p).ToList<Person>();

匹配:0

SELECT dbo.Person.PersonID,
       dbo.Person.txtFirstName,
       dbo.Person.txtLastName,
       dbo.Person.txtAccount
FROM [dbo].[Person]
INNER JOIN dbo.CustomFieldData_Person ON dbo.Person.PersonID = dbo.CustomFieldData_Person.PersonID
INNER JOIN dbo.CustomFieldData ON dbo.CustomFieldData_Person.CustomFieldDataID = dbo.CustomFieldData.CustomFieldDataID
INNER JOIN dbo.CustomFieldListData ON dbo.CustomFieldData.CustomFieldDataID = dbo.CustomFieldListData.CustomFieldDataID
INNER JOIN dbo.CustomListItem ON dbo.CustomFieldListData.CustomListItemID = dbo.CustomListItem.CustomListItemID
WHERE [dbo].[Person].[InstanceID] = 80

匹配:142

    List<Person> result =
        (from p in DB_Instance_Singleton.getInstance.Person
         join cfdp in DB_Instance_Singleton.getInstance.CustomFieldData_Person on p.PersonID equals cfdp.PersonID
         join cfd in DB_Instance_Singleton.getInstance.CustomFieldData on cfdp.CustomFieldDataID equals cfd.CustomFieldDataID
         join cfld in DB_Instance_Singleton.getInstance.CustomFieldListData on cfd.CustomFieldDataID equals cfld.CustomFieldDataID
         join cli in DB_Instance_Singleton.getInstance.CustomListItem on cfld.CustomListItemID equals cli.CustomListItemID
         where (p.InstanceID == iInstanceID)
         select p).ToList<Person>();

匹配:142

WHERE子句中,我似乎对实体CustomListItem的列有问题。

但我不明白为什么

LINQ查询的结果不等于SQL查询的结果

感谢JamieD77,我发现了我的错误。LINQ-SQL是正确的,但在我的c#代码中,我交换了两个参数。