开发者

Linq left outer join across multiple tables retrieving info from each table

I am new to linq and want to perform joins across three tables. The sql equivalent of what I am trying to do is:

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

The linq code I have developed is this, but these don't seem to be outer join as it is开发者_如何学编程 not returning any results unless I comment out the joins to the address and county table

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
                             };

Any help would be greatly appreciated,

Thanks, Neil

EDIT:

I also tried using the 'into' statement but then on my second join the alias isn't recognised.

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

So for the c.newIddressID.Id equals a.new_addressId I get this error message:

The name 'c' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.


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
                             };

See how I modified the join for a and cy so I could add the DefultIfEmpty method.

I split the two outer joins in to one query and then join that query back into the original query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜