2014-07-14 74 views
0

嗨,我是使用4個表不以某些空表工作..LINQ multible加入

有時不具備此表中(教育或地址或證書) 信息,所以結果來了空.. 如果有信息我怎麼說? 例如

**PersonTable** 

PersonId PersonName 
1453   george 

**Certificate Table** 

CertificateId PersonId CertificateName (0 records) 


**Addres Table** 

AddresId PersonId Adress 
1   1453  ...... 
2   1453  ..... 
3   1453  ...... 
4   1453  ...... 
5   1453  ........ 

**EducationTable** 

EducationId PersonId Education 
1    1453   school name 


**BankTable** 

BankId  PersonId   Bank (0 records) 


Person table = 1 records 
Certificates = 0 records 
Address = 5 records 
Education = 1 records 
Bank = 0 records 

var query = (from p in DbEntities.t_Person 
         join q in DbEntities.t_Address on p.PersonId equals q.addressId 
         join n in DbEntities.t_Education on p.PersonId equals n.EduId 
         join j in DbEntities.t_Certificate on p.PersonId equals j.CertificateId 
         join f in DbEntities.t_BankAccount on p.PersonId equals f.BankId 
         where p.PersonId.Equals(id) 
         select new { p, q, n, j, f }).FirstOrDefault(); 

返回查詢值零,

我不明白爲什麼這些價值?

Person table = 1 records 
Address = 5 records 
Education = 1 records 

我想過這個問題,但沒有

var query = (from p in DbEntities.t_Person 
        where p.PersonId.equlas(id) 
        select p).singleOrDefault(); 

query = from p in dbEntities.Address 
       WHERE P.AddressId.equlas(query.PersonId) 
      select p; 

回答

0

這是因爲你在做內部連接,這將只返回記錄,如果所有的加盟條件都滿足!看起來你想要做一個LEFT JOIN,它將返回左表中的所有行,並且從右表返回找到的任何記錄,否則返回null。 Here是一個很好的文章,解釋了不同類型的連接,包括圖表!

Linq-to-Entities進行左連接的方式是DefaultIfEmpty()。有關使用示例,請參見MSDN

+0

謝謝你jeff ... – user3816319

+1

沒問題!如果這是您尋求的解決方案,請標記爲答案。 –