Linq在多个表中保留外连接,从每个表中检索信息

时间:2011-06-23 09:06:33

标签: linq

我是linq的新手,想要在三个表中执行连接。我试图做的sql相当于:

SELECT PM.Id, PM.new_clientId, .....
C.new_firstname, c.new_surname,.....
A.new_addressid, A.new_addressline1, A.new_addressline2.....
CY.new_county

FROM partialMatch PM
INNER JOIN new_client C ON PM.new_clientId = C.new_clientId
LEFT OUTER JOIN new_address A ON C.new_addressId = A.new_addressId
LEFT OUTER JOIN new_county CY ON A.new_countyId = CY.new_countyId

WHERE PM.ownerId = @UserId AND PM.new_reviewed <> true

我开发的linq代码是这样的,但是这些似乎不是外连接,因为它没有返回任何结果,除非我注释掉地址和县表的连接

var partialMatches = from pm in ContextService.CreateQuery<new_partialmatch>()
                             join c in ContextService.CreateQuery<new_client>()     on pm.new_ClientId.Id   equals c.new_clientId
                             join a in ContextService.CreateQuery<new_address>()    on c.new_AddressID.Id   equals a.new_addressId
                             join cy in ContextService.CreateQuery<new_county>() on a.new_CountyID.Id equals cy.new_countyId
                             where pm.OwnerId.Id == _currentUserId && pm.new_Reviewed != true

                             select new
                             {
                                 Id = pm.Id,
                                 new_ClientID = pm.new_ClientId,
                                 new_MatchingCRMClientID = pm.new_MatchingCRMClientId,
                                 new_MatchingVulcanClientID = pm.new_MatchingVulcanClientID,
                                 new_name = pm.new_name,
                                 firstname = c.new_FirstName,
                                 surname = c.new_Surname,
                                 dob = c.new_DateOfBirth,
                                 addressId = a.new_addressId,
                                 address1 = a.new_AddressLine1,
                                 address2 = a.new_AddressLine2,
                                 address3 = a.new_AddressLine3,
                                 address4 = a.new_AddressLine4,
                                 county = cy.new_County
                             };

非常感谢任何帮助,

谢谢, 尼尔

编辑:

我也尝试使用'into'语句,但是在我的第二次加入时,别名无法识别。

from pm in ContextService.CreateQuery<new_partialmatch>()
                             join c in ContextService.CreateQuery<new_client>()     on pm.new_ClientId.Id   equals c.new_clientId into pmc
                             from x in pmc.DefaultIfEmpty()
                             join a in ContextService.CreateQuery<new_address>()    on c.new_AddressID.Id   equals a.new_addressId

因此,对于c.newIddressID.Id等于a.new_addressId,我收到此错误消息:

名称'c'不在'equals'左侧的范围内。考虑交换'equals'两侧的表达式。

1 个答案:

答案 0 :(得分:1)

var addressCountryQuery = from a in ContextService.CreateQuery<new_address>()
                          from cy in ContextService.CreateQuery<new_county>().Where( cy => cy.new_countyId == a.new_CountyID.Id).DefaultIfEmpty()
                          select new
                          {
                              addressId = a.new_addressId,
                              address1 = a.new_AddressLine1,
                              address2 = a.new_AddressLine2,
                              address3 = a.new_AddressLine3,
                              address4 = a.new_AddressLine4,
                              county = cy.new_County
                          }

var partialMatches = from pm in ContextService.CreateQuery<new_partialmatch>()
                     join c in ContextService.CreateQuery<new_client>()     on pm.new_ClientId.Id   equals c.new_clientId
                     from a in addressCountryQuery .Where( a => a.addressId == c.new_AddressID.Id).DefaultIfEmpty()
                             where pm.OwnerId.Id == _currentUserId && pm.new_Reviewed != true

                             select new
                             {
                                 Id = pm.Id,
                                 new_ClientID = pm.new_ClientId,
                                 new_MatchingCRMClientID = pm.new_MatchingCRMClientId,
                                 new_MatchingVulcanClientID = pm.new_MatchingVulcanClientID,
                                 new_name = pm.new_name,
                                 firstname = c.new_FirstName,
                                 surname = c.new_Surname,
                                 dob = c.new_DateOfBirth,
                                 addressId = a.addressId,
                                 address1 = a.AddressLine1,
                                 address2 = a.AddressLine2,
                                 address3 = a.AddressLine3,
                                 address4 = a.AddressLine4,
                                 county = a.County
                             };

看看我是如何修改a和cy的连接所以我可以添加DefultIfEmpty方法。

我将两个外部联接分成一个查询,然后将该查询加回到原始查询中。