1

我正在努力将 SQL 查询转换为 LINQ。

这是SQL:

SELECT dbo.ORGANIZATION.ORGANIZATION_ID, 
       dbo.ORGANIZATION.ORGANIZATION_NAME, 
             dbo.PWR_ORGANIZATION_DIRECTORY.DIRECTORY_IDENTIFIER, 
             dbo.DETAIL_REQUIREMENT.DETAIL_ID, 
       dbo.DETAIL_REQUIREMENT.DETAIL_IDENTIFIER, 
             dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.DIRECTORY_ATTRIBUTE, 
             dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.ATTRIBUTE_VALIDATION, 
       dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.ATTRIBUTE_TRANSFORMATION, 
             dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.DETAIL_ACCESS

FROM  dbo.ORGANIZATION INNER JOIN
      dbo.DETAIL_REQUIREMENT ON dbo.ORGANIZATION.ORGANIZATION_ID = dbo.DETAIL_REQUIREMENT.ORGANIZATION_ID INNER JOIN
      dbo.PWR_ORGANIZATION_DIRECTORY INNER JOIN
      dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING ON dbo.PWR_ORGANIZATION_DIRECTORY.ORGANIZATION_ID = dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.ORGANIZATION_ID AND 
      dbo.PWR_ORGANIZATION_DIRECTORY.DIRECTORY_TYPE_ID = dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.DIRECTORY_TYPE_ID AND 
      dbo.PWR_ORGANIZATION_DIRECTORY.DIRECTORY_ID = dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.DIRECTORY_ID ON 
      dbo.DETAIL_REQUIREMENT.DETAIL_ID = dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.DETAIL_ID AND 
      dbo.ORGANIZATION.ORGANIZATION_ID = dbo.PWR_ORGANIZATION_DIRECTORY_MAPPING.ORGANIZATION_ID

WHERE dbo.ORGANIZATION.ORGANIZATION_ID = 0 AND 
      dbo.PWR_ORGANIZATION_DIRECTORY.DIRECTORY_ID = 1 AND 
            dbo.PWR_ORGANIZATION_DIRECTORY.DIRECTORY_TYPE_ID = 1

这就是我开始的方式,但是当表之间有两个和三个连接时感到困惑:

var megaFetch = from org in context.Organizations
            join detReq in context.DetailRequirements on org.OrganizationId equals detReq.OrganizationId
            join detMap in context.OrganizationDirectoryMappings on org.OrganizationId equals detMap.OrganizationId &&

有人可以在这里指导我吗?

谢谢。

4

1 回答 1

2

如果您需要连接多列中的表,请创建具有相同字段数的两侧的匿名类型。应该是这样的;

new {col1 = x.col1, col2 = x.col2, ...} equals new { col1 = y.col1, col2 = y.col2, ...}
于 2013-10-31T15:09:20.777 回答